代码之家  ›  专栏  ›  技术社区  ›  user8105388

如何将条目保存到结构

  •  0
  • user8105388  · 技术社区  · 7 年前

    @IBAction func press(_ sender: Any) {
        contacts.append(Person(name: a.text!, surname: b.text!  , phone: Int(c.text!)!))
        print(self.contacts.description)
    }
    
    struct Person {
        var name: String
        var surname: String
        var phone: Int
    }
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   Aks    7 年前

    您可以创建一个person数组,并在有人按下按钮时附加到该数组中。下面是斯威夫特游乐场的一个例子。

    struct Person {
        var name: String
        var surname: String
        var phone: Int
    }
    var contacts = [Person]()
    
    func press(name: String, surname: String, phone: Int) {
        contacts.append(Person(name: name, surname: surname, phone: phone))
        print(contacts.description)
    }
    
    press(name: "Aks", surname: "Homes", phone: 123)
    press(name: "Harry", surname: "Potter", phone: 124)
    press(name: "Ron", surname: "Weisly", phone: 345)
    press(name: "Shan", surname: "Wate", phone: 456)