Playground located at: https://github.com/ToniNichev/Swift-Data-Types
-
Arrays
Arrays store data of the same type in the same order (sorted)
– line 1 demonstrates creating an array syntax.
– line 7 shows the shorthand array creation syntax
– line 13 shows creating an array with Array literal (and default values)
[csharp highlight=”2,7,13″]
// create empty array
var myArrayOne = Array<String>()
myArrayOne.append(“Test Value”)
print(“Array one value: \(myArrayOne[0])”)
// or using shorter syntax
var myArrayTwo = [Int]()
myArrayTwo.append(1)
myArrayTwo.append(2)
print(“Array one value: \(myArrayTwo[1])”)
// or with default values using array literal
var myArrayThree = [“one”, “two”, “three”]
print(“Array one value: \(myArrayThree[1])”)
// or
var myArrayFour = Array(repeating: 5, count: 3)
print(“Array count: \(myArrayThree.count)”)
//iterating over an array
for item in myArrayTwo {
print(item)
}
[/csharp]
result:
Array one value: Test Value Array one value: 2 Array one value: two Array count: 3 1 2
Although arrays can only store elements of the same type, it is still possible to store elements from different types by creating array of Any type.
In the example below, an array of Any type is created, and populated with string, integer, double values and even another array. Then while iterating through the array elements we convert all values to string representation (line 9), and print them.
var mixedArray = Array<Any>() mixedArray.append("This is string") // adding string mixedArray.append(123) // adding integer mixedArray.append(2.64) // double value mixedArray.append(myArrayTwo) // adding another array print("Printing values of mixed array, converted to strings") for item in mixedArray { let val: String = String(describing: item) print("\(val)") }
[csharp highlight=”9″]
var mixedArray = Array<Any>()
mixedArray.append(“This is string”) // adding string
mixedArray.append(123) // adding integer
mixedArray.append(2.64) // double value
mixedArray.append(myArrayTwo) // adding another array
print(“Printing values of mixed array, converted to strings”)
for item in mixedArray {
let val: String = String(describing: item)
print(“\(val)”)
}
[/csharp]
result:
Printing values of mixed array, converted to strings This is string 123 2.64 [1, 2]
-
Sets
Sets unlike arrays could only store unique values in unsorted order
[csharp highlight=”2,7,13″]
// create empty set
var mySetOne = Set<String>()
mySetOne.insert(“one”)
mySetOne.insert(“two”)
mySetOne.insert(“three”)
print(“mySetOne does contain’two’ : \(mySetOne.contains(“two”)) “)
// iterating
for item in mySetOne {
print(“mySetOne: \(item)”)
}
// we could create sets using Array literal too
var mySetTwo: Set&amp;amp;amp;amp;lt;String&amp;amp;amp;amp;gt; = [“One”, “two”]
[/csharp]
result:
mySetOne does contain'two' : true mySetOne: one mySetOne: three mySetOne: two
-
Dictionaries
Dictionaries are pretty much like arrays, but they store values against keys
[csharp]
// dictionary with integer values
var myDict = [String: Int]()
myDict[“one”] = 1
myDict[“two”] = 2
print(“myDict value at positon ‘one’: \(myDict[“one”]!)\n”)
// dictionary with different types of objects
var obj = [String: AnyObject]()
obj[“one”] = “12345” as AnyObject?
// velue two, contains second dictionary
obj[“two”] = [“user”: [“name”: “Jim”, “email”: “jim@yahoo.com”] ] as AnyObject?
obj[“three”] = “1221” as AnyObject?
// value ‘four’ is Integer
obj[“four”] = 25 as AnyObject
//getting second dictionary inside value ‘two’
var subDict = obj[“two”]! as! [String: AnyObject]
print(“\n’subDict’ value: \(subDict)”)
// retreiving specific value
var subDict_user_name = ((obj[“two”]! as! [String: AnyObject])[“user”] as! [String: AnyObject])[“email”] as! String
print(“\n’subDict_user_name’ value: \(subDict_user_name)”)
[/csharp]
result:
myDict value at positon 'one': 1 'subDict' value: ["user": { email = "jim@yahoo.com"; name = Jim; }] 'subDict_user_name' value: jim@yahoo.com
-
Enumerations
Swift enumerations are common set of related cases
[csharp]
enum Locations {
case Bulgaria, USA, Australia
var address: String {
switch self {
case .Bulgaria:
return “Ohrid 3 b”
case .USA:
return “5711 Jefferson st”
case .Australia:
return “New zeland drive”
default:
return “No selection!”
}
}
var city: String {
switch self {
case .Bulgaria:
return “Velico Tarnovo”
case .USA:
return “West New Tork”
case .Australia:
return “Sidney”
default:
return “No selection!”
}
}
}
let country = Locations.USA
print(“COUNTRY: \(country) \nAddress: \(country.address) \nCity:\(country.city)”)
[/csharp]
result:
COUNTRY: USA Address: 5711 Jefferson st City:West New Tork
-
Closures
[csharp]
let s = double(2, 2)
testFunc(ParamA: 2, closureA: { (paramB) in
print(“>>>\(paramB)”)
}) {
print(“Closure 2”)
}
}
// a closure that has no parameters and return a String
var hello: () -> (String) = {
return “Hello!”
}
// a closure that take two parameters and return an Int
var double: (Int, Int) -> (Int) = { x, y in
return y * x
}
func testFunc(ParamA:Int, closureA:(_ paramB:Int)->(), closureB:()->()) {
let result = 2 * ParamA
closureA(result)
closureB()
}
[/csharp]
Playground located at: https://github.com/ToniNichev/Swift-Data-Types