/**
16.4-可选链调用下标索引
*/
/**
可选值?属性
可选值?方法
*/
class MyStringHash {
subscript (x: String) -> Int {
return x.hashValue - 1000
}
}
var msh = MyStringHash.init()
print(msh["abc"])
var mshop: MyStringHash?
mshop = msh
print(mshop?["abc"])
/**
打印
4799450059707600744
Optional(4799450059707600744)
*/
var array: [Int]? = [1,2,3,4,5,6]
print(array?[2])
/**
Optional(3)
*/
原文链接:https://www.f2er.com/swift/322343.html