2014WWDC just ended, the personal feel the biggest surprise is swift, so in the first time to download the official development documents and the latest SDK, Xcode tried to write code, here as a starting point, hoping to inspire everyone's enthusiasm for learning, After all, as a procedural ape the most exciting is not to be able to keep themselves happy to learn new knowledge.
Swift's start:
HelloWorld is very simple.
println ("Hello, World")
Direct use of println without the use of OC Traditional @ symbol, and the semicolon is not required, instantly feel swift more love
Define variables:
Use the Let keyword to define a constant, use the var keyword to define the variable, the type of the constant does not have to be specified at compile time, but you must perform an exact assignment for it so that you can use the constant in a lot of places.
?
| 1 2 3 4 5 |
var myvariable = 42//If a variable in Swift has already been declared as int then the following is the int type     println (myvariable)//Output 42, there is no use of NSLog (@ "%@", myvariable); very simple answer.     myvariable = 50 //Assign this variable again     let myconstant = 42//define a constant     println (" myvariable=\ (myvariable) ")//here is the format output operation, but only need to precede the output variable with a backslash, and put the variable in parentheses (must be placed in parentheses) |
The type of a constant or variable you do not need to indicate in each display, the type is actually the type you assign to the variable for the first time (not Complie, but at runtime). When defining variables, use var myvariable = 42 Actually at compile time the type of the myvariable variable is ' () ' can be understood as void type, the runtime will be based on all the values of the corresponding run-time check to determine the type of variable, such as the above example.
If the initial value does not provide enough type-judged information (no initialization assignment), you can write the declaration of the type behind the variable and use: separate and assign the value
?
| 1 |
let explicitDouble: Double = 70 //对explicitDouble变量进行赋值并且指定这个变量类型是Double |
Output operation:
1. Type conversion
?
| 1 2 3 |
1 let label = "The width is " //定义一个变量接受一个字符串 2 let width = 94 //定义一个变量接受int类型数据3 let widthLabel = label + String(width)//将int进行强制转换和字符串一起输出,如果想要将一个类型的数据转换为另一种类型,需要显示的调用转换方法 |
2. Formatted output
?
| 1 2 3 4 |
1 let apples = 3 //int变量 2 let oranges = 5 3 let appleSummary = "I have \(apples) apples." //进行格式化输出,输出的格式是将需要输出变量变成格式\(变量)4 let fruitSummary = "I have \(apples + oranges) pieces of fruit." |
Multiple variables for continuous output of the words directly splicing can be I have \ (apples) \ (oranges) apples.
Of course, in order to increase the readability of the output log is generally added to the delimiter, I have \ (apples) +\ (oranges) apples.//in the inside can add the string directly, as long as it is not in the variable body can be
Array & Dictionary
There are two ways to define an array
The first way
var shoppinglist = ["Catfish", "water", "Tulip", "Blue Paint", "" "///Direct definition and not display the specified type is a collection, the compiler self-type detection, easy to understand
The second way
structmutablecollectionsliceable
The important point of Swift is that without mutable and non-mutable, you can directly modify the data in the collection
SHOPPINGLIST[1] = "bottle of Water"//modify the data in the array directly
println (shoppinglist[4])//fatal Error:array index out of range, have to mention that the error is more accurate, you can directly locate the wrong location, and even though this "" is also counted in the collection, and participated in the calculation of the length, but the direct printing does not print the results
Dictionary
There are two different ways of defining the same
The first way of defining
var OCC = [ "Mylcolm": "Captain", "Kaylee": "Mechanic",//This comma does not write can be ]
The second way of defining dictionary type:structhashable, valuetype>
var textocc:dictionary = [ "Mylcolm": "Captain", "Kaylee": "Mechanic",//This comma does not write can be ]
If you want to add a dictionary element directly to the operation
textocc["Jayne"] = "public"//is equivalent to adding a Dictionary object Jayne:key:value very intuitive.
Create an empty array and dictionary
1 Let Emptyarray = string[] () 2 Let emptydictionary = Dictionary<string, float> ()
Directly with the Java thing, although just beginning to learn swift, but feel swift absorbs a lot of the advantages of the old language, some features of OC, some of Java's generic ideas, it is estimated that the program will be more secure.