Swift Learning-constants-variables-use of data types (ii)

Source: Internet
Author: User

I. Arrays in Swift

//Arrays in Swift//Nsarray *arr = @["", "" "];//the difference between a and OC array is to remove the previous @Let Arr11 = ["Zhangsan","Lisi"]print (ARR11)//iterate through each of the elements forAinchArr11 {print (a)}//print like OCPrint (Arr11 asNsarray)//strings are saved in the arrayLet arr = ["Zhangsan","Lisi"]//The NSObject is saved in the arrayLet arr1 = ["Zhangsan",1]//definition can only hold array of string typesvar array: [String]//initializing an arrayArray = ["Zhangsan"]//adding elementsArray.append ("Lisi") print (array)//Delete ElementArray.removeatindex (1) print (array)//Delete all elementsArray.removeall (keepcapacity:true) print (array.capacity)//Notice the change of the array capacity forIinch 0.. <Ten{array.append ("\ (i)") Print ("\ (array)---\ (array.capacity)")}//instantiate a new arrayvar array2 =[String] () array2.append ("1") Array2.append ("2")//Stitching ArraysArray + =array2print (Array)

Two. Dictionaries in Swift

//Create a dictionary//@{@ "key": "Value"};//Create a dictionary and create an array in swift, using the []//[String:nsobject] Type is the most common format used for iOS development in the futureLet dict = ["name":"Zhang San"," Age": -]//Get Valuedict["name"]//Traverse Dictionary forKeyinchdict.keys{Print (Dict[key])}//traversing a dictionary with a tuple type automatically assigns the key in the dictionary high School to the first variable in the tuple, assigning value to the second variable in the tuple//Note: The first one is key, the second value for(KK, VV)inchdict{print (KK) print (vv)}var dict2=[String:anyobject] ()//New Key/vale//if the corresponding key is not in the dictionary, it will be addedDict2 = ["name":"John Doe"," Age": -]dict2["Sex"]="Mans"Dict2//set the same key, the previous value will be overwrittendict2["name"] ="Money Six"Dict2//DeleteDict2.removevalueforkey ("name") Dict2//Merging Dictionariesvar dict3 = ["score": -] for(Key,value)inchdict3{Dict2[key]=Value}dict2var Dict4= ["Address":"Xiamen"] for(Key,value)inchdict4{Dict2.updatevalue (value, Forkey:key)}dict2

Three. Strings in Swift

//a string in Swift//define strings in OC with @ "Swift" "//Note: The string in OC is the string in NSString Swift//the string in OC is an object that inherits from the NSObject//the string in Swift is a struct//The performance of strings in Swift is higher than in OCvar str:string ="Zhang San"//Stitchingvar str2 ="Cool"Str+=str2//FormattingLet NUM8 = -Let name="Wangwu"//target: Name = Wangwu, age =//you can use \ () to stitch other values in Swiftvar STR3 ="name = \ (name), age = \ (NUM8)"//09:08:07//Note: All values must be placed in the array, even if only one value needs to be placed in the arrayvar STR4 = String (format:"%02d:%02d:%02d", arguments: [9,8,7])//Intercept//Note: In SWI, if you need to intercept a string, it is recommended to use the string in OC, because the string interception method of string in Swift is very complex to use .//strings in Swift can be easily converted with strings in OCvar str5:nsstring ="Zhang Sanha haha"var str6="Zhang San reply sent"  asNsstringvar subStr= Str5.substringwithrange (Nsmakerange (1,3))//strings in Swift are supported for direct traversalvar STR7 ="www:baidu.com" forCinchstr7.characters{Print (c)}

Four. Functions in Swift

//functions in Swift/*Swift defines the format of the function: semantics: Returns the value of the structure returned by the previous computed value, func function name (parameter list), return value {execute code}*///parameters with no return value//If the function does not return a value, write void//If the function does not return a value, it can be shortened//1. () Replace void//2. Can omit ()->voidFunc Say ()void{Print ("Hello World")}say () func say2 ()-() {print ("Hello World")}say2 () func say3 () {print ("Hello")}//There are no parameters for return valuesFunc GetNumber ()int{return 999}print (GetNumber ())//There are no return values for parameters//-(void) sum: (int) a B: (int) b//Swift2.0, the parameter name starting with the second parameter in the formal parameter is automatically made into a sticky note for improved readability//not until Swift2.0.func sum (a:int, b:int) {print (a+b)} SUM (TenB: -)//Add tags, add external parameters//x/y is called an external parameter, A/b is called internalfunc sum2 (x a:int,y b:int) {print (a+b)} SUM2 (x:TenY: -)//There are parameters with return valuesFunc sum4 (A:int, B:int)int{returnA +B}print (SUM4 (TenB: -))

Swift Learning-constants-variables-use of data types (ii)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.