Here are some of the basic syntax of Swift 2.0, written in the playground, each time the swift version with the new, open the look, you can compare where changes have occurred.
Contents include: 01 Variables & Constants 02 Branches 03 loops 04 String 05 array 06 Dictionary 07 functions
01 Variables & Constants
//: Playground-noun:a Place where people can playImport Uikitvar str="Hello, playground."/*: let constant var variable * automatic ' derivation ', able to derive the exact type of the variable according to the code on the right*/Let x= -//constants cannot be modified once you set a value//x =var y= $y= -Print (x+y)/*: Swift has strict data type requirements * The default type of integer is int (long 64 bits) * The default type of decimal is Double (CGFloat used more in OC) * Swift does not do ' Implicit conversion ', if you want to evaluate different types of data, you must convert*/Let i=TenLet J=8.5//Let k = i + jLet INum = i +Int (j) Let Dnum= Double (i) +Jlet Size= Cgsizemake ( -, -) Let W= CGFloat (j) + Size.width
02 Branches
//: Playground-noun:a Place where people can playImport Uikitvar str="Hello, playground."Let i= -/*: 1. The condition does not require brackets 2. {} cannot omit*/ifi > -{print ("OK")}/*: * OC and C non-zero-true * Swift in logical value only two True/false*/ifi = = -{print ("OK")}//three mesh, grammar and OC basically the sameLet B = i < -?Ten: the/** Optional option (Optional)? Indicates that a variable can have a value, or it can have no value * convenience init? Constructor,? Indicates that the object is not necessarily instantiated * '! ' means that the programmer tells the compiler that the object must have a value, that the compilation can pass * If run, no value, ' Crash '*/Let URL= Nsurl (string:"http://www.baidu.com")//If you are unsure if the URL really has a value, add an IfifURL! =Nil {Let request= Nsurlrequest (url:url!)}print (URL)//determine if the object has a value-added if, can be guaranteed to enter {} code, URL1 must have valueifLet URL1 = Nsurl (string:"Http://www.baidu.com/s?wd=zhang") {Let request=nsurlrequest (URL:URL1)}
03 Cycles
//: Playground-noun:a Place where people can playImport UIKit forvar i =0; I <Ten; i++{print (i)}/*: Swift * 0~9 where 0..<10 cannot have spaces*/ forIinch 0.. <Ten{print (i)}//0~10 forIinch 0...Ten{print (i)}//do not care about subscript, you can use _ Omit for_inch 0.. <Ten{print ("Hello World")}
04 String
//: Playground-noun:a Place where people can playImport UIKit/*: Using string in Swift is a struct, high efficiency, supports traversing OC nsstring, is an object * Swift, the conversion between String and NSString is easy*/var str="I'm going to fly higher."//traverse characters in Swift 2.0 forCinchstr.characters {print (c)}//string concatenationLet i =1Let rect= CGRectMake (0,0, -, -) Str= str +"\ (i)"+"\ (rect)"//when stitching strings, be sure to remember if they are optional, pay special attention, take Optionalvar x:int?x=TenLet str1="\ (x!)"//formatLet frmstring = String (format:"%02d:%02d:%02d", arguments: [ One,1, -])/*: If using Range, it is best to use NSString*///identical to the original string.var subStr = Str.substringwithrange (range<string.index>(Start:str.startIndex, End:str.endIndex)) print (SUBSTR)//where to start, where to endSUBSTR = Str.substringwithrange (range<string.index> (Start:advance (Str.startindex,3), End:advance (Str.startindex,6)) print (SUBSTR) let mystr:nsstring=Strlet myStr1= str asNsstringmystr.substringwithrange (Nsmakerange (0,3))
05 Arrays
Import Uikitvar str="Hello, playground."/*: array definition []*/Let array= ["Zhangsan","Lisi"]//Traverse forStrinchArray {print (str)}print (array[0]) print (array[1])//cannot add content to an immutable group//array.append ("Wangwu")/*: variable var and immutable let * OC in an array that can hold any type of object*///type of array1 [string], automatically deduced, only allows strings to be storedvar array1 = ["Xiaohua","Xiaocao"];//array2 Type [nsobject], can hold ' arbitrary object '//in Swift, numbers can also be stored directly as objects in an array, without the need for ' convert 'var array2 = ["Xiaohua","Xiaocao",123];array1.append ("Laozhang") Array2.append ( -)/*: The definition of an array*///Array3 is an array that allows only strings to be stored, defining the typevar array3: [String]//the array3 is instantiated, and the operation is not allowed untilArray3 =[String] () array3.append ("Laowang")//Merging of arraysArray3 + =array1//must be of the same type to be able to merge//tip: In swift development, the types of objects that are normally stored in arrays are the same! //Array3 + = Array2//deletion of ArraysArray3.removeatindex (2) Array3array3+=array1print (Array3.count)//clears the array keepcapacity whether to maintain capacityArray3.removeall (keepcapacity:false)//Add Content//If you append an element to an array that exceeds the capacity, it is directly based on the existing capacity * 2//OC instantiates a variable group, preferably specifying a capacity forIinch 0.. < -{array3.append ("Hello-\ (i)") print (Array3[i]) print (array3.capacity)}
06 Dictionaries
//: Playground-noun:a Place where people can playImport Uikitvar str="Hello, playground."/*: Define dictionary [] let immutable var variable*///[String:nsobject]-types most common//JSON data format, key is String//[Key:value]var dict = ["name":"Zhang San"," Age": -]dict//If key does not exist, a newdict["title"] ="boss"Dict//If key exists, it will overwritedict["name"] ="Lisi"Dict/*: The dictionary traversal K, V can be casually written, preceded by key, followed by value*/ for(VV, KK)inchdict {print (vv+"---\ (KK)")}/*: Merging Dictionaries*///1. Define a dictionaryvar dict1: [String:nsobject]dict1=[String:nsobject] () dict1["Nickname"] ="Lao Wang"dict1[" Age"] = the//2. Merge-If there is a overwrite, if there is no new for(k, V)inchDict1 {dict[k]=v}dict
07 Functions
" Hello, playground. " // parameter and return value, cancellation of # Func sum in 1.2(Num1 a:int, num2 b:int), int{ return A +10< /c10> Void { ten)
Swift 2.0 Basic syntax