main.swift//array////Created by Mac on 15-9-9.//Copyright (c) 2015 young apples. All rights Reserved.//import foundation//immutable group, defined by let var array:[string] = ["ss", "Set", "Jim"]println ("\ (array)") var Array1:[float] = [23,45,36];p rintln ("\ (array1)")//define an empty array var array2:[string] = []println ("\ (array2)") var array3 = [ array,array1,array2]//array Add contents array + = ["NME"]println ("\ (array)") var a = "Shei" Array + = [A]array + = [A]array + = [A]array + = [A]println ("\ (array)") Array.append ("Shine") println ("\ (array)")//Modify an element within an array array[0] = "Ikmage" println ("\ (array)")/ /modify the specified position, specify the length of the element array[0...1] = ["sine", "Kmse"]println ("\ (array)")//Specify position Insert element Array.insert ("Zhang", atindex:2) println ("\ (array)")//delete the element at the specified location Array.removeatindex (2) println ("\ (array)")//delete the last element Array.removelast () println ("\ (array)" ////deletes the specified length of element//array.removerange (Range (0...3))//println ("\ (array)")//////removes all elements//array.removeall ( Keepcapacity:true)//println ("\ (array)")//array traversal, way one//first define element, traverse the for in method to traverse var a1 = "" For A1 in array{println ("\ (A1)") }For loop traversal for var i = 0;i<array.count;++i{println ("\ (Array[i])")}//shows the traversal of the health value pair for (Index,value) in enumerate (array) { println ("\ (index): \ (value)")}//the definition of the immutable array let array4:[string] = ["Seni", "Wlsi"]println ("\ (ARRAY4)") var a2 = "" For A2 In array4{println ("\ (A2)")}
Dictionary:
main.swift//dictionary////Created by Mac on 15-9-9.//Copyright (c) 2015 young apples. All Rights Reserved.//import foundation//dictionary is unordered var dic:[string:string] = ["Key1": "Value1", "Key2": "Value2"]println ("\ \ (DIC) ") var A1: (Nsinteger) = dic.count//Remove the key's health value team dic[" Key1 "] = Nilprintln (" \ (DIC) ")//Add Health value to dic[" key1 "] =" value1 " println ("\ (DIC)")//Modify the specified key valuedic["Key1"] = "Zhang" println ("\ (DIC)")//Create an empty dictionary var dic1:[string:string] = [:] println ("\ (DIC1)")//determine if the health value pair is empty, then remove if var key = Dic.removevalueforkey ("Key1") {Dic.removevalueforkey ("Key1") println ( "\ (DIC)")}//determine if the health value is present if var key = dic["Key1"]{println ("\ (key)")}else{println ("empty")}//remove all health values to//dic.removeall (keepcap Acity:true)//println ("\ (DIC)")//Dictionary traversal, Dictionary is unordered for (key,value) in dic{println ("\ (key): \ (value)")}var A5 = [:]for a5 In dic{println ("\ (A5)")}//Traverse all keyfor key in dic.keys{println ("\ (key)")}var a3 = "" for A3 in dic.keys{ println ("\ (A3)")}//traverse all valuefor value in dic.values{println ("\ (value)")}vaR a4 = "" for A4 in dic.values{println ("\ (A4)")}//Remove all Keyvar Arraykey = Array (Dic.keys) println ("\ (Arraykey)")//Remove all VA Luevar arrayvalue = Array (dic.values) println ("\ (arrayvalue)")
How to use the loop:
main.swift// Cycle Practice -01//// Created by Mac on 15-9-8.// Copyright (c) 2015 young apples. All rights reserved.//import Foundationvar j = 0//FOR loop structure mode for J in (0...15) {println ("\ (j)") }for var k = 0;k< 100;++k{ println ("\ (k)") }for var k1 = 0;k1<100;k1=k1+2{ println ("K1 ==\ (K1)") }//Create a range that does not contain a cap for var i1 = 0;i1>=0;++i1{ if i1 > 1000{ break } println ("i1=\ (I1)")}//while cycle var i2 = 0while i2& lt;100{ i2++; println ("i2=\ (I2)")}//do while loop var i3 = 0do{i3++ println ("i3=\ (i3)")}while I3<100var i4=0for var j1=0;j1< 10;j1=j1+1{for var j2=0; j2<10; ++j2{ i4++; println ("\ (i4)") } }
The use of arrays and dictionaries in swift, and the use of Loops (code written on the XCODE6 version)