Swift-Optional&Equatable&访问控制权限

2020-12-31  本文已影响0人  浪的出名

Optional

var age : Int?
var age : Optional<Int>
var age : Int? = 10

switch age {
    case .none :
        print("nil")
    case .some(10) :
        print(10)
    default :
        print("default")
}

Optional解包

强制解包

if let&guard let

if let tmp = age {
    print(tmp)
}else{
    print("nil")
}

func test() {
    
    guard let guardTmp = age else {return}
    
    print(guardTmp)
}

test()

Equatable

var age = 10
var age1 : Int = 10

var isEqual = age == age1
print(isEqual)
// 返回结果为true
struct XQTeacher : Equatable{
    var age : Int
    var name : String
}

var t = XQTeacher(age: 18, name: "xq")

var t1 = XQTeacher(age: 19, name: "xq")

var t2 = XQTeacher(age: 18, name: "xq")

print(t == t1)
print(t == t2)
// 返回结果为false true 
class XQTeacher : Equatable{
    static func == (lhs: XQTeacher, rhs: XQTeacher) -> Bool {
        return lhs.age == rhs.age && lhs.name == rhs.name
    }
    
    
    var age : Int
    var name : String
    
    init(age : Int , name :String) {
        self.age = age
        self.name = name
    }
}

var t = XQTeacher.init(age: 18, name: "xq")

var t1 = XQTeacher.init(age: 19, name: "xq")

print(t == t1)

访问控制权限

上一篇下一篇

猜你喜欢

热点阅读