1, dictionary-Dictionary (that is, key-value pairs)
var empty = [String:int] ()// Create an empty dictionary var mydic = ["Name": "Hangge", "url": "hangge.com"] //Declare a dictionary mydic[" Address "] =" China "//Add or modify key value Mydic.removevalueforkey (" name ") //delete" name "This key value mydic[" name "] = Nil //can also be deleted "Name" This key value Mydic.keys //Access Dictionary key collection Mydic.values//Access Dictionary values collection//Traverse dictionary for (key,value) in Mydic { print ("\ ( Key): \ (value) ");} Only the key of the dictionary is traversed (key) for key in Mydic.keys { print ("\ (key)");}//Only the value of the dictionary is traversed (value) for value in Mydic.values { print ("\ ( Value) ");
2, arrays- array
var types = ["None", "Warning", "error"] //Omit type array declaration var menbers = [String] ()//Declare an empty array menbers.append ("Six ") //add element Menbers + = [" seven "]//Add element Menbers.insert (" one ", atindex:0) //Specify location add Element Menbers[0] =" message " / /Modify the data in the array by subscript menbers[0...2] = ["Message", "Hangge", "com"] //Replace the data by a small standard interval (first 3 data) Menbers.count / /Gets the number of array elements menbers.isempty //Determine if the array is empty menbers.removeatindex (2) //Delete the array labeled 2 menbers.removelast () //Delete last element Menbers.removeall (keepcapacity:true) //Delete all elements in array let Addstringarr = types + menbers//array combination //Use the for in to implement an array traversal for the value in menbers{ print ("\ (value)");} All indexes of the array are traversed at the same time through the enumerate function with data for (Index,value) in Menbers.enumerate () { print ("Index: \ (index) data: \ (value)");}
3, struct-struct
Create a struct struct bookinfo{ var id:int = 0 var name:string = "Defaut" var author:string = "Defaut"} var Book1:bo Okinfo//default constructor Create struct instance var book2 = BookInfo (id:0021,name: "Aerial Song", Author: "Hangge") //Call the constructor to create an instance book2.id = 1234 // modifying internal values
4, enum-enum
Enum CompassPoint {case west}var directiontohead = compasspoint.west Enum Planet:int {case Mercury = 1 case Venus = 2 case Earth = 3}let Earthsorder = Planet.Earth.rawValue//ra Wvalue to get his original value: 3let possibleplanet = Planet (rawvalue:2) //through the original value to find the corresponding enumeration member: Venus enum Direction {case up Case -down func description (), string{ switch (self), {case . Up: return ' up ' case . Down: return "downward" } }}print (Direction.Up.description ())
Transferred from: http://www.hangge.com/blog/cache/detail_515.html
Swift Dictionary and array