Print appended struct (swift4)

╄→尐↘猪︶ㄣ 提交于 2019-12-20 06:26:46

问题


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

class ViewController: UIViewController {
    @IBOutlet var c: UITextField!
    @IBOutlet var a: UITextField!
    @IBOutlet var b: UITextField!
    var contacts: [Person] = []

    @IBAction func press(_ sender: Any) {
        contacts.append(Person(name: a.text!, surname: b.text!  , phone: Int(c.text!)!))
        print(ViewController.Person)
    }

    struct Person {
        var name: String
        var surname: String
        var phone: Int
    }
}

回答1:


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.




回答2:


You are trying to print the actual struct of Person. You want to print a specific instance.

You can print the whole array as:

print("contacts: \(contacts)")

If you want to print the one instance, I would suggest splitting your code a little:

@IBAction func press(_ sender: Any) {
    let person = Person(name: a.text!, surname: b.text!  , phone: Int(c.text!)!)
    print(person)
    contacts.append(person)
}

But to make the printing useful you should add the description property to your struct and make your struct conform to CustomStringConvertible.

struct Person: CustomStringConvertible {
    var name: String
    var surname: String
    var phone: Int

    var description: String {
        return "Person name: \(name), surname: \(surname), phone: \(phone)"
    }
}

You should also learn to safely deal with optionals. The ! operator means "crash if not nil". Your code will crash if the user types in "Hello" into the phone field.

@IBAction func press(_ sender: Any) {
    if let name = a.text, let surname = b.text, let phoneStr = c.text, let phone = Int(phoneStr) {
        let person = Person(name: name, surname: surname, phone: phone)
        print(person)
        contacts.append(person)
    }
}

And also consider that a phone number is not a numeric value. Use String, not Int to store phone numbers.




回答3:


print(ViewController.Person) is wrong

Please use:

print(contacts.last)


来源:https://stackoverflow.com/questions/46614785/print-appended-struct-swift4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!