12/08/2018, 17:18
[iOS] [Swift] Tổng hợp tất cả từ khóa trong ngôn ngữ Swift (Part 3)
Part 1: https://viblo.asia/p/ios-swift-tong-hop-tat-ca-tu-khoa-trong-ngon-ngu-swift-part-1-E375zEAdlGW Part 2: https://viblo.asia/p/ios-swift-tong-hop-tat-ca-tu-khoa-trong-ngon-ngu-swift-part-2-naQZRwrvlvx Expressions and Types Keywords Any : đại diện cho bất kỳ kiểu nào của đối tượng, bao ...
Part 1: https://viblo.asia/p/ios-swift-tong-hop-tat-ca-tu-khoa-trong-ngon-ngu-swift-part-1-E375zEAdlGW
Part 2: https://viblo.asia/p/ios-swift-tong-hop-tat-ca-tu-khoa-trong-ngon-ngu-swift-part-2-naQZRwrvlvx
Expressions and Types Keywords
Any : đại diện cho bất kỳ kiểu nào của đối tượng, bao gồm cả hàm.
var anything = [Any]()
anything.append("Any Swift type can be added")
anything.append(0)
anything.append({(foo: String) -> String in "Passed in (foo)"})
as : dùng để ép kiểu để có thể truy xuất được thuộc tính hoặc phương thức của kiểu đó. Có thể ép đúng kiểu hoặc sai kiểu.
var anything = [Any]()
anything.append("Any Swift type can be added")
anything.append(0)
anything.append({(foo: String) -> String in "Passed in (foo)" })
let intInstance = anything[1] as? Int
hoặc
var anything = [Any]()
anything.append("Any Swift type can be added")
anything.append(0)
anything.append({(foo: String) -> String in "Passed in (foo)" })
for thing in anything {
switch thing {
case 0 as Int:
print("It's zero and an Int type")
case let someInt as Int:
print("It's an Int that's not zero but (someInt)")
default:
print("Who knows what it is")
}
}
false : biến kiểu Bool, ko phải là true.
let alwaysFalse = false
let alwaysTrue = true
if alwaysFalse { print("Won't print, alwaysFalse is false