創建一個名稱結構。名稱包含兩部分名字(也稱爲給定的 姓名或個人姓名)和姓氏(家人或姓氏)。名稱結構應該 實現可比較協議。Swift 3可比協議
我用下面的代碼嘗試,但我不知道如何包含給定名稱,personalName,familyName和姓在比較
struct Name : Comparable {
let firstName : String
let lastName: String
init(firstName : String, lastName:String) {
self.firstName = firstName
self.lastName=lastName
}
}func < (lhs: Name, rhs: Name) -> Bool {
return lhs.firstName < rhs.firstName && lhs.lastName < rhs.lastName
}func == (lhs: Name, rhs: Name) -> Bool {
return lhs.firstName == rhs.firstName && lhs.lastName == rhs.lastName
}
let firstName = Name(firstName: "Paul", lastName: "Smith")
let givenName = Name(firstName: "Paul" , lastName: "Smithe")
let personalName = Name(firstName: "Paul", lastName: "Smith")
print (firstName == givenName)
您可能要修改你的問題,並已經提供了你試過。 – Zico