Swift - Collection Types

Arrays

var employeeNo = [Int]() //empty array let response: [String] = ["True", "False"] //immutable array var profession = ["Doctor","Engineer","Scientist"] //mutable array

Example

var cities = ["NewYork","Dallas","LosAngeles"] let cityCount = cities.count if cities.isEmpty { print("No city defined") } else{ print(String(cityCount) + " cities defined") }

Dictionaries

var roomDetails: [Int: String]

Example

var roomDetails = [101:"Jennifer", 103:"Karen", 201:"Mark"] func getName(roomNo: Int)-> String { if let name : String = roomDetails[roomNo] { return name } return "Room is vacant" } print(getName(roomNo: 201))

Sets

var menu = Set<String>()

Example

var sports: Set = ["Tennis", "Golf", "Football"] sports.insert("Cricket")

No comments:

Post a Comment