【发布时间】:2021-12-30 05:24:53
【问题描述】:
这是我的朋友班:
class Friend {
var firstName: String = ""
var lastName: String = ""
var age: Int = 0
var description:String = ""
init(firstname: String, lastname: String, age: Int) {
self.firstName = firstname
self.lastName = lastname
self.age = age
}
}
这是我应该在 viewDidLoad 函数中声明和实例化 5 个 Friend 对象并将它们添加到“friendList”数组中的地方。
import UIKit
class ViewController: UIViewController {
var friendsList: [Friend] = []
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
friendsList.append("John", "Doe", 20)
}
}
Swift 在“friendsList.append”行告诉我“在调用实例方法 'append' 时没有完全匹配”。
【问题讨论】:
-
您的代码中没有任何内容会创建
Friend对象,您必须在某个地方执行此操作。 -
你可以在 append 函数中创建你的
Friend例如:friendsList.append(Friend(firstname: "John", lastname: "Doe", age: 20))