2016-09-18 176 views
-1

創建一個名稱結構。名稱包含兩部分名字(也稱爲給定的 姓名或個人姓名)和姓氏(家人或姓氏)。名稱結構應該 實現可比較協議。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) 
+0

您可能要修改你的問題,並已經提供了你試過。 – Zico

回答

3

在美國,默認是lastName先進行排序,firstName第二。你可以改變這一點,如果它在你的國家不同:

struct Name: Comparable { 
    var firstName: String 
    var lastName: String 

    static func == (lhs: Name, rhs: Name) -> Bool { 
     return (lhs.firstName == rhs.firstName) && (lhs.lastName == rhs.lastName) 
    } 

    // Provide the ordering for 2 names by comparing lastName first, for example: 
    //  Jane Austen < Alan Smith 
    //  Alan Smith < John Smith 
    static func < (lhs: Name, rhs:Name) -> Bool { 
     if lhs.lastName != rhs.lastName { 
      return lhs.lastName < rhs.lastName 
     } else if lhs.firstName != rhs.firstName { 
      return lhs.firstName < rhs.firstName 
     } else { 
      return false 
     } 
    } 
} 
+0

我應該忽略問題中提及的surName familyName personalName和givenName嗎? – Hadeel

+0

你是什麼意思?姓氏,姓氏和姓氏都是用英語表示的同一件事:你從你父親那裏繼承的名字的一部分。同樣,名字=給定的名字 – Jenny

+0

我知道,但因爲他們在問題中提到,我想到枚舉類型 – Hadeel

1
class ViewController: UIViewController, myprotocol{ 

    @IBOutlet weak var lbl1: UILabel! 
    @IBOutlet weak var text1: UITextField! 

    func testfirst(info:String) { 

     lbl1.text = info as String 
    } 




    override func viewDidLoad() { 
     super.viewDidLoad() 


    } 

     @IBAction func btnshow(_ sender: UIButton) 
    { 

     let storyBoard = UIStoryboard(name: "Main", bundle: nil) 

     let svc = storyBoard.instantiateViewController(withIdentifier: "secondViewController") as! secondViewController 
     svc.delegate = self 

     svc.str = text1.text! 



     self.navigationController?.pushViewController(svc, animated: true) 

    } 

2)

protocol myprotocol { 

    func testfirst(info:String) 
} 

3)

class secondViewController: UIViewController { 


    var delegate : myprotocol? 

    @IBOutlet weak var lbl2: UILabel! 
    @IBOutlet weak var txt2: UITextField! 


    var str = String() 


    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view. 

     lbl2.text = str 
    } 



    @IBAction func btn2(_ sender: UIButton) 
    { 
     if (delegate != nil) 
     { 
      let information:String = lbl2.text!+txt2.text! 

      delegate?.testfirst(info: information) 

      self.navigationController?.popViewController(animated: true) 
     } 
    }