本文介绍了打印附加结构(swift4)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有三个用于将数据附加到结构中的文本。如何打印我附加的内容?现在我收到一条错误消息。
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
}
}
推荐答案
我修改了你的代码,你可以使用它,它会给你你想要的结果。
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
}}
我刚刚在print语句中做了一处更改。由于您的联系人数组在您查看控制器本身,因此您可以自己直接使用它并使用功能描述来打印更新联系人的值。
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.
这篇关于打印附加结构(swift4)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!