// MARK: overloaded operator
class Person {
    var name: String = ""
    var age: Int = 0
    var cardId: Int = 0

    init(name: String.age: Int.cardId: Int) {
        self.name = name
        self.age = age
        self.cardId = cardId
    }
}

var p1 = Person(name: "zhangsan", age: 10, cardId: 1) // 0x107857fd0
var p2 = Person(name: "zhangsan", age: 10, cardId: 1) // 0x107858000

if p1 = = = p2 {
    print("p1 === p2")}Copy the code

Print does not print because the addresses of the two persons in memory are different.

Swift supports overloaded operators, and == inherits Equatable.


class Person: Equatable {
    / / rewrite = =
    static func = = (lhs: Person.rhs: Person) -> Bool {
        lhs.cardId = = rhs.cardId
    }
}

if p1 = = p2 {
    print("p1 == p2")}// p1 == p2

Copy the code

Swift also provides a Comparable protocol, inherited from Equatable.

    static func < (lhs: Self.rhs: Self) -> Bool
    static func < = (lhs: Self.rhs: Self) -> Bool
    static func > = (lhs: Self.rhs: Self) -> Bool
    static func > (lhs: Self.rhs: Self) -> Bool

Copy the code

Class comparisons can be implemented by overriding operators


static func < (lhs: Person.rhs: Person) -> Bool {      
    lhs.age < rhs.age
}

Copy the code

The comparison of struct

Swift struct is a value type that can be directly compared to ==, but > inherits Comparable.


struct Animal: Comparable {
    static func < (lhs: Animal.rhs: Animal) -> Bool {
        lhs.age < rhs.age
    }

    static func = = (lhs: Animal.rhs: Animal) -> Bool {
        lhs.age = = rhs.age
    }
    var name: String
    var age: Int
}

var a1 = Animal(name: "dog", age: 1)
var a2 = Animal(name: "cog", age: 2)
if a1 < a2 {
    print("a1 < a2")}// a1 < a2

Copy the code

Swift constructor

While the previous Person is done by overriding the init() constructor, we can define other constructors in Swift

A failable constructor


init?(name: String) {
    if name.isEmpty {
        return nil
}
    self.name = name
}

var p3 = Person(name: "")
if p3 = = nil {
    print("person init fail") // Prints because name is invalid, returns nil
}
print(p3)// nil

var p4 = Person(name: "123")
if let p44 = p4 {
    print("person init success")}else {
    print("person init fail")}// log
/ / ▿ Optional < Person >
// ▿ some: 
      

Copy the code

Through the init? The constructed class is an enumeration of Optional type, which is used by the swift? We know that we’re constructing an optional type of Person.

Convenience constructor


convenience init(a) {
    self.init(name: "zhangsan", age: 1, cardId: 1)}Copy the code

The convenience constructor must call the specified constructor of the same class.

Specifying the constructor


class student: Person {
    var subName: String = ""
    init(subName: String) {
        super.init(name: "z", age: 1, cardId: 1)
        self.subName = subName
    }
}

Copy the code

Specifies that the constructor must call the constructor of the parent class

Construct classes from literals

We can use the name of the Person to initialize a Person instance object, this requires ExpressibleByStringLiteral inheritance


class Person: Comparable.ExpressibleByStringLiteral {
    typealias StringLiteralType = String
    required init(stringLiteral value: String) {
        self.name = value
        self.age = 2
        self.cardId = 2}}var p5: Person = "zhaowu"
print(p5.age) / / 2

Copy the code