1. Array-Array
var types = ["none", "warning", "error"] // Omit type array declaration
var menbers = [String] () // Declare an empty array
menbers.append ("six") // Add elements
menbers + = "seven" // Add elements
menbers.insert ("one", atIndex: 0) // Add element at the specified position
menbers [0] = "message" // Modify the data in the array by subscript
menbers [0..2] = "message" // Replace the data by the sub-scale interval (the first 3 data)
menbers.count // Get the number of array elements
menbers.isEmpty // Determine whether the array is empty
menbers.removeAtIndex (2) // Delete the array with subscript 2
menbers.removeLast () // Remove the last element
menbers.removeAll (keepCapacity: true) // Remove all elements in the array
var addStringArr = types + menbers // array combination
// Use for in to achieve array traversal
for value in menbers {
println ("\ (value)");
}
// Use the enumerate function to traverse all indexes and data of the array at the same time
for (index, value) in enumerate (menbers) {
println ("Index: \ (index) Data: \ (value)");
}
2. Dictionary-Dictionary (that is, key-value pair)
var empty = Dictionary <String, Int> // Create a space dictionary
var myDic = ["name": "hangge",
"url": "hangge.com"] // Declare a dictionary
myDic ["address"] = "china" // Add or modify the key value
myDic.removeValueForKey ("name") // Remove the key value of "name"
myDic ["name"] = nil // The key value of "name" can also be deleted
myDic.keys // Access the key collection of the dictionary
myDic.values // Access the dictionary's values collection
// Traverse the dictionary
for (key, value) in myDic {
println ("\ (key): \ (value)");
}
// Only traverse the keys of the dictionary (key)
for key in myDic.keys {
println ("\ (key)");
}
// Only traverse the value of the dictionary (value)
for value in myDic.values {
println ("\ (value)");
}
3. Structure-struct
1
2
3
4
5
6
7
8
9
10
// Create a structure
struct BookInfo {
var ID: Int = 0
var Name: String = "Defaut"
var Author: String = "Defaut"
}
var book1: BookInfo // The default constructor creates a structure instance
var book2 = BookInfo (ID: 0021, Name: "Hangge", Authoer: "hangge") // Call one by one constructor to create an instance
book2.ID = 1234 // Modify internal value
4. Enumeration-enum
enum CompassPoint {
case North
case South
case East
case West
}
var directionToHead = CompassPoint.West
enum Planet: Int {
case Mercury = 1
case Venus = 2
case Earth = 3
}
let earthsOrder = Planet.Earth.toRaw () // toRaw () method to get his original value: 3
let possiblePlanet = Planet.fromRaw (2) // The fromRaw () method uses 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 "Down"
}
}
}
println (Direction.Up.description ())
Swift-Description of complex data types (arrays, dictionaries, structures, enumerations)
label:
Original address: http://www.cnblogs.com/Free-Thinker/p/4838083.html