let maximumNumberOfLoginAttempts = 10
var currentLoginAttempt: Int
var : Có thể thay đổi giá trị
let : Không thể thay đổi giá trị
Swift tự suy luận kiểu của giá trị khi khai báo
let someString = "Some string literal value"
var emptyString = ""
var anotherEmptyString = String()
let multilineString = """
These are the same.
"""
Nó sẽ in ra màn hình đúng như những gì ta nhập vào kể cả khoảng trắng. Nếu muốn viết kí tự đặc biệt thì đặt sau dấu \
var variableString = "Horse"
variableString += " and carriage"
let catCharacters: [Character] = ["C", "a", "t", "!", "🐱"]
let catString = String(catCharacters)
print(catString)
// Prints "Cat!🐱"
let string1 = "hello"
let string2 = " there"
var welcome = string1 + string2
// welcome now equals "hello there"
var instruction = "look over"
instruction += string2
// instruction now equals "look over there"
let unusualMenagerie = "Koala 🐨, Snail 🐌, Penguin 🐧, Dromedary 🐪"
print("unusualMenagerie has \(unusualMenagerie.count) characters")
// Prints "unusualMenagerie has 40 characters"
let greeting = "Guten Tag!"
greeting[greeting.startIndex]
// G
greeting[greeting.index(before: greeting.endIndex)]
// !
greeting[greeting.index(after: greeting.startIndex)]
// u
let index = greeting.index(greeting.startIndex, offsetBy: 7)
greeting[index]
// a
Thuộc tính endIndex trỏ đến sau vị trí sau phần tử cuối cùng của String
Thuộc tính .indices dùng để lấy chỉ mục. Nếu for bình thường nó sẽ lấy giá trị
Không thể truyền trực tiếp số nguyên vào ví dụ greeting[1] để lấy giá trị 'u'
for index in greeting.indices {
print("\(greeting[index]) ", terminator: "")
}
// Prints "G u t e n T a g ! "
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"
var names = ["Mic", "Sam", "Christine"]
var names = ["Mic" : 1, "Sam" : 2, "Christine" : 3]
var favoriteGenres: Set = ["Rock", "Classical", "Hip hop"]
var names: [String] = []
var lookup: [String: Int] = [:]
var letters = Set<Character>()
Các thuộc tính truy cập sửa xoá khá quen thuộc với những ngôn ngữ lập trình cơ bản
Các phần tử trong Set không lặp lại
Set có một số thuộc tính xem trong docs swift nếu cần Bấm vào Đây
let names = ["Anna", "Alex", "Brian", "Jack"]
for name in names {
print("Hello, \(name)!")
}
// Hello, Anna!
// Hello, Alex!
// Hello, Brian!
// Hello, Jack!
for index in 1...5 {
print("\(index) times 5 is \(index * 5)")
}
let names = ["Anna", "Alex", "Brian", "Jack"]
let count = names.count
for i in 0..<count {
print("Person \(i + 1) is called \(names[i])")
}
// Person 1 is called Anna
// Person 2 is called Alex
// Person 3 is called Brian
// Person 4 is called Jack
for name in names[2...] {
print(name)
}
// Brian
// Jack
for name in names[..<2] {
print(name)
}
// Anna
// Alex