Print appended struct (swift4)

前端 未结 3 1092
孤独总比滥情好
孤独总比滥情好 2021-01-29 01:15

I have three textifleds that I am using to append data into a struct. How can I print what I appended? Right now I am getting a error message.

import UIKit

cla         


        
3条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-29 01:34

    I have modified your code, you can use it it will give you result what you want.

    import UIKit
    
    class ViewController: UIViewController {
    @IBOutlet var c: UITextField!
    @IBOutlet var a: UITextField!
    @IBOutlet var b: UITextField!
    var contacts: [Person] = []
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    
    
    }
    @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
    
    }}
    

    I have just made one change in print statement. As your contacts array in you view controller itself so you can directly use it by self and use function description for printing updating values of contacts.

提交回复
热议问题