Here's a brief introduction to Swift's basic usage:
println ("Hello, world!") println ("Hello, world!") ") //defines a constant, using the keyword" let "//in Swift, almost all of the basic data types or object types used by the struct to rewrite let Haidian =" Haidian "/Let haidian:string =" Haidian "  ; //in Swift, the system will do the type inference for you, the previous line of code is the same as this line//\ (variable name) convert the variable or constant name to a string for output println ("Haidian = \ (Haidian)")//"\ ( Variable/constant name) "can convert variable/constant to string output//Direct output println (Haidian) //define a variable using the keyword" var "var hello =" Hello, "hello = hello + Haidian //String overloading: String stitching println ("Hello = \ (hello)") //Swift supports almost all Unicode encoded characters except for mathematical symbols, Arrows and dashes) to name the variable/constant let dog = "dog," println (dog) var = dog + "Love You" println () //defines an integral type (type inference) var a = Ten //defines a Integral type, in fact this statement, the compiler will infer for us: var a:int = 10, the format is: "Variable/constant name: Type", in Swift the integer is Int, if the system is 32 bits, then this variable is Int32, if the system is 64 bits, then is Int64, If I define a 8-bit integer tree, you need to explicitly specify Int8:var b:int8 = //to explicitly specify the number of digits of int var C = 12.0 &NB Sp //in swift, floating-point data defaults to Doublevar d = Double (b) + C //In Swift, implicit conversions are not supported, and if you want to manipulate two different types of data, you need to explicitly convert their Type, conversionThe format is: "Type name (constant/variable name)" //tuple, the tuple borrowed from the relational database inside the concept, the relational database inside a data is a tuple, the tuple can hold different types of data, and the structure of similar, But structs need to be declared first, tuples can be created directly using//define a tuple let ErrorCode = (404, "not Fount") //This is an (Int, String) class Type of tuple, equivalent to: let ErrorCode: (Int, String) = (404, "not fount")//element decomposition (remove elements from tuple)//Remove elements inside tuples (by subscript) println ("ErrorCode = \ (err orcode.0), errormessage = \ (errorcode.1)) //can create a tuple by giving the element declaration type name in the tuple let Errorcodea = (errorcode:404, ErrorMessage: "Not Found")//remove element println by type name ("ErrorCode = \ (errorcodea.errorcode), errormessage = \ ( Errorcodea.errormessage) println (Errorcodea) //if the element corresponding to the tuple's location is ignored, you can use "_" Let (StatusCode, _) = Errorcodeaprintln ("StatusCode = \ (StatusCode)") //array: In the swift array, only the same type of data can be stored in order to access operations on the arrays. If the data type is not the same, it is converted to Nsarray to handle //using VAR to declare a mutable array, var Namearraya = ["Zhang San", "John Doe", "Harry"] //equivalent: Var nam earraya:string[] = ["Zhang San", "John Doe", "Harry", "Zhao Liu"]println ("Namearraya = \ (Namearraya)") //define an empty array var somevalues = [Int] () //declares an empty array, the elements inside the array are Int, the version before Xcode6 BETA4, the notation is: int[] () var namearrayc = ["Hello", 123] //add Element Namearraya.append ("Zhao Liu") println ("Namearraya = \ (Namearraya)") to the array // Replace the element inside the array namearraya[0] = "First" println (Namearraya) //Delete the element inside the array namearraya.removeatindex (0) println (Namearraya) //Delete the last element Namearraya.removelast () println (Namearraya) //define the immutable array, the mutable and immutable arrays are declared by Let and Var, Before Xcode6 beta4, immutable groups (constant arrays) cannot add and remove elements, but they can make changes to elements, but after Xcode6 Beta4, the constant array cannot change the elements inside let Namearrayd = ["Zhang San", "John Doe", "Harry"]/ /namearrayd[0] = "Replace" for name in Namearrayd { println ("name = \ (name)")} //both the element inside the array and the subscript where the element is located, which need to be used Enumerate (array name) method for (index, name) in enumerate (Namearrayd) { println ("index = \ (index), name = \ (name)")}&NB sp; //dictionary, in Swift, the dictionary all key is the same type, all value is the same type, key and value can be different types, if not the corresponding same type, it is automatically converted to nsdictionary//in OC Declare an empty dictionary var dictionarystudent = dictionary<string, float> () var person = ["Name":"Hui elder brother", "sex": "male"] //equivalent: var person:dictionary<sting, string> = ["Name": "Hui elder brother", "sex": " Male "]println (" person = \ (person) ") //Dictionary printing is unordered //add key value to dictionary person[" habit "] =" smoking "PR Intln ("person = \ (person)")//If the dictionary does not have this key, then add directly, if the key already exists, then directly replace the previous value person["name"] = "Bogota" println (" person = \ (person))//update the value of a key, update with Updatevalue (value, Forkey:key), and return the old value before update let OldValue = Person.updatevalue ("Trico", Forkey: "Name") println ("person = \ (person), OldValue = \ (oldValue)")//Dictionary traversal for (key, value) in P Erson { println ("key = \ (key), value = \ (value)") the variable and immutable} //dictionaries are also controlled by let and Var, and cannot be updated on a constant dictionary & nbsp; //Control statement: If,if...else, while, do...while,switch...casevar num = 3var condition = true//if num { &N Bsp //without parentheses, the subsequent judging condition can only be bool type, integer and other types are not allowed// &NBSP;PRINTLN ("This is integral type")//}if condition { println ("This is a bool value")} for var i = 0; I < 10;i++ { println ("i = \ (i)")} for J in 0..<3 { ///". < "includes the left side, not including the right, before the Xcode6 Beta4 version, using" ... " println ("j = \ (j)")} for J in 0...4 { //"..." includes both the left and the right PRI Ntln ("j = \ (j)")} //switch, in Swift, has made great changes to switch, and the default does not need to write Breakvar number = 20switch number {case 10:& nbsp println ("number = ten") Case 20: println ("number = +") default: println ("other")} // If you want to enforce the next statement, you need to use the Fallthrough keyword, this statement must be a condition-satisfying var Numbera = 10switch Numbera {case 10: println ("number = 10" ) Fallthroughcase 20: println ("number = +")/ fallthrough &NB Sp //equivalent effect of no break in OC default: println ("other") } //case is a range var Numberb = 12switch Numberb {case 1..<10: println ("This is a single number") Case 10..<100: println ("This is a double digit") Default:&nbSp println ("This is another number") the range of}//case can be crossed, but only the first case statement satisfying the condition is executed var Numberc = 12switch Numberc {case 1..<20: println ("Between 1~20") Case 10..<100: println ("Between 10~100") default: println ("other")} // can be double-judged (using the "where" keyword) var numberd = 13switch Numberd {case 1..<20 where Numberd < 15: println ("Nu Mberd = ") Case 10..<100: println (" double digit ") default: println (" other numbers ")} // Determine where the point is in the coordinate system, "_": ignores the value of the corresponding position in the tuple var dot = (3, 5) switch points {case (0, 0): println ("origin") case (_, 0): &N Bsp println ("x-axis") Case (0, _): println ("Y-axis") default: println ("within quadrant")} var Pointa = (0, 4) Switch Pointa {case (0, 0): println ("origin") case (Let x, 0): println ("x-axis, x = \ (x)") case (0, let y) : println ("y-axis, y = \ (y)") default: println ("within quadrant")} //optionals, optional variable, If you use Optionals to declare a variable, the value of the variable may be empty (nil). (Attention to unpacking/unpack)//only nil in Swift means null, no Nil,null,null,nsnull var value:int? “?” Indicates that this is an optional variable, the value may be nil let numberstr = "123456.3" value = Numberstr.toint () if nil! = value { & nbsp After Xcode6 Beta6, Int? Can not be used as a condition of judgement before (direct value) println ("with value: value = \ (value)")} var Valuea = 21let Valueb = Valuea + value! //unpacking/unpacking, if there is no value, you cannot unpack println ("Valueb = \ (VALUEB)")
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Basic syntax for the swift development tutorial--swift