The Switf programming Language 練習代碼(5)

//swift

//  main.swiftapp

//  classTestless

//spa

//  Created by 小強 on 15/12/8.3d

//  Copyright © 2015 小強. All rights reserved.code

//orm


import Foundationthree


var x = 0.0, y = 0.0, z = 0.0ci


let possibleNumber = "123";unicode

let convertedNumber = Int(possibleNumber);


print(possibleNumber);

print(convertedNumber);



if (convertedNumber != nil)

{

    print("\(possibleNumber) has an integer value of \(convertedNumber!)");

}

else

{

    print("\(possibleNumber) could not be converted to an integer");

}


//if let actualNumber = Int(possibleNumber)

//{

//    print("\(possibleNumber) has an integer value of \(actualNumber).");

//}

//else

//{

//    print("\(possibleNumber) could not be converted to an integer.");

//}


let possibleString: String? = "An optional string.";

print(possibleString!);


let assumeString: String! = "An implicitly unwrapped optional string.";

print(assumeString);


let age = 3;

assert(age > 0, "A person's age cannot be less than zero");


let name = "world";


if name == "world" {

    

    print("hello, world")

    

} else {

    

    print("對不起,\(name),我不認識你!")

    

}


name == "world" ? print("hello world") : print("對不起,\(name),我不認識你");


for index in 1 ..< 5

{

    print("\(index) * 5 = \(index * 5)");

}


var string:Array = ["D", "o", "g", "!"];


for character in string{

    

    print(character);

    

}


print("count of string is " + "\(string.count)");

print("capacity of string is " + String(string.capacity));


let normal = "Could you help me,please?";

let shouty = normal.uppercaseString;

print(shouty);


let whispered = normal.lowercaseString;

print(whispered);


//let dogString = "Dog!???";

//for codeUnit in dogString.unicodeScalars

//{

//    print("\(codeUnit.value)");

//}


var shoppingList: [String] = ["eggs", "Milk"]


if shoppingList.isEmpty {

    

    print("The shopping list is empty.");

    

} else {

    

    print("The shopping list is not empty.")

    

}


shoppingList.append("Flour");

shoppingList += ["Baking Powder"]


print(shoppingList);


var firstItem = shoppingList[0];

print(firstItem);

shoppingList[0] = "Six eggs";

print(shoppingList);


//shoppingList[4..<6 ] = ["Bananas", "Apples"];


shoppingList.insert("Maple Syrup", atIndex: 0);


print(shoppingList);



let mapleSyrup = shoppingList.removeAtIndex(0);

print(mapleSyrup);

print(shoppingList);



for (index, value) in shoppingList.enumerate(){

    

    print("Item \(index + 1): \(value)")

    

}


var someInt = [Int]();


print("someInts is of type Int[] with \(someInt.count) items. ");


var threeDoubles = [Double](count: 3, repeatedValue: 0.0);


print(threeDoubles);


var anotherThreeDoubles = Array(count: 3, repeatedValue: 2.5);

print("anotherThreeDoubles = \(anotherThreeDoubles)");


var sixDoubles = threeDoubles + anotherThreeDoubles;

print("The sixDoubles is \(sixDoubles)");

相關文章
相關標籤/搜索