-
Notifications
You must be signed in to change notification settings - Fork 4
Swift Xcode
Mingtao edited this page Aug 5, 2019
·
31 revisions
Cmd 0: hide/show left panel
Cmd 1: show project navigator
Cmd Option 0: hide/show right panel
Option left-click a file: Open file side by side
Cmd Shift F: Search within file / file name
Cmd Shift O: Open file
Option left-click a variable: Check its type
let greeting = "Hello world!"
let firstChar = greeting[greeting.startIndex] // 'H'
let secondCharIndex = greeting.index(greeting.startIndex, offsetBy: 1)
let secondChar = greeting[secondCharIndex] //'e'
let lastCharIndex = greeting.index(before: greeting.endIndex)
let lastChar = greeting[lastCharIndex] //'!'
let secondLastCharIndex = greeting.index(greeting.endIndex, offsetBy: -2)
let secondLastChar = greeting[secondLastCharIndex] //'d'
var welcome = "hello"
welcome.insert("!", at: welcome.endIndex)
// welcome now equals "hello!"
welcome.insert(contentsOf: " there", at: welcome.index(before: welcome.endIndex))
// welcome now equals "hello there!"
welcome.remove(at: welcome.index(before: welcome.endIndex))
// welcome now equals "hello there"
let range = welcome.index(welcome.endIndex, offsetBy: -6)..<welcome.endIndex
welcome.removeSubrange(range)
// welcome now equals "hello"
let greeting2 = "Hello,world!"
let index = greeting2.firstIndex(of: ",") ?? greeting2.endIndex
let excludeIndexSubstring = greeting2[..<index] //"Hello" of type String.SubSequence
let includeIndexSubstring = greeting2[...index] //"Hello," of type String.SubSequence
let newString = String(excludeIndexSubstring) //"Hello" of type String
var someInts = [Int]()
someInts.append(3)
var threeDoubles = Array(repeating: 0.0, count: 3)
// threeDoubles is of type [Double], and equals [0.0, 0.0, 0.0]
var anotherThreeDoubles = Array(repeating: 2.5, count: 3)
// anotherThreeDoubles is of type [Double], and equals [2.5, 2.5, 2.5]
var sixDoubles = threeDoubles + anotherThreeDoubles
// sixDoubles is inferred as [Double], and equals [0.0, 0.0, 0.0, 2.5, 2.5, 2.5]
var shoppingList = ["Eggs", "Milk"]
shoppingList += ["Baking Powder"]
shoppingList += ["Chocolate Spread", "Cheese", "Butter"]
shoppingList.insert("Maple Syrup", at: 0)
let mapleSyrup = shoppingList.remove(at: 0)
let apples = shoppingList.removeLast()
for item in shoppingList {
print(item)
}
for (index, value) in shoppingList.enumerated() {
print("Item \(index + 1): \(value)")
}
A type must be hashable in order to be stored in a set
var letters = Set<Character>()
let count = letters.count
let isEmpty = letters.isEmpty
letters.insert("a")
letters = [] // letters is now an empty set, but is still of type Set<Character>
var favoriteGenres: Set<String> = ["Rock", "Classical", "Hip hop"]
var favoriteGenres2: Set = ["Rock", "Classical", "Hip hop"] //Simpler than above line
let removedGenre = favoriteGenres.remove("Rock")
let contains = favoriteGenres.contains("Funk")
for genre in favoriteGenres.sorted() {
print("\(genre)")
}
let oddDigits: Set = [1, 3, 5, 7, 9]
let evenDigits: Set = [0, 2, 4, 6, 8]
let singleDigitPrimeNumbers: Set = [2, 3, 5, 7]
oddDigits.union(evenDigits).sorted() // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
oddDigits.intersection(evenDigits).sorted() // []
oddDigits.subtracting(singleDigitPrimeNumbers).sorted() // [1, 9]
oddDigits.symmetricDifference(singleDigitPrimeNumbers).sorted() // [1, 2, 9]
let houseAnimals: Set = ["๐ถ", "๐ฑ"]
let farmAnimals: Set = ["๐ฎ", "๐", "๐", "๐ถ", "๐ฑ"]
let cityAnimals: Set = ["๐ฆ", "๐ญ"]
houseAnimals.isSubset(of: farmAnimals) // true
farmAnimals.isSuperset(of: houseAnimals) // true
farmAnimals.isDisjoint(with: cityAnimals) // true. To determine whether two sets have no values in common.
A dictionary Key type must conform to the Hashable protocol, like a setโs value type.
var namesOfIntegers = [Int: String]()
namesOfIntegers[16] = "sixteen"
namesOfIntegers = [:] // namesOfIntegers is once again an empty dictionary of type [Int: String]
let count = namesOfIntegers.count
let isEmpty = namesOfIntegers.isEmpty
var airports: [String: String] = ["YYZ": "Toronto Pearson", "DUB": "Dublin"]
var airports2 = ["YYZ": "Toronto Pearson", "DUB": "Dublin"] //Simpler format than above line
airports["LHR"] = "London"
let oldValue = airports.updateValue("Dublin Airport", forKey: "DUB") //oldValue is of type string?
airports["APL"] = nil //Remove a key-value pair
let removedValue = airports.removeValue(forKey: "DUB") //Another way to remove a key-value pair
for (airportCode, airportName) in airports {
print("\(airportCode): \(airportName)")
}
for airportCode in airports.keys {
print("Airport code: \(airportCode)")
}
for airportName in airports.values {
print("Airport name: \(airportName)")
}
let airportCodes = [String](airports.keys) // airportCodes is ["LHR", "YYZ"]
let airportNames = [String](airports.values) // airportNames is ["London Heathrow", "Toronto Pearson"]