这就是我的现状:
class FirstViewController: UITableViewController {
...
}
protocol SharedFunctions {
func createEvent(event: Event, text: String)
}
extension FirstViewController: SharedFunctions {
createEvent(event: Event, text: String) {
...
}
}
class SecondViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var sharedFunctions = SharedFunctions? // < Xcode error...
@IBAction func postChatMessageAction(_ sender: Any) {
self.sharedFunctions.createEvent(event: event, text: "New Event")
}
...
}
错误:类型名称后面的预期成员名称或构造函数调用
当我更改我的代码时,正如Xcode所暗示的那样,错误已经消失,但是我得到了createEvent函数上的一个错误
var sharedFunctions = SharedFunctions?.self //Xcode suggestion is adding .self
现在我得到了一个关于createEvent函数的错误
@IBAction func postChatMessageAction(_ sender: Any) {
self.sharedFunctions.createEvent(event: event, text: "New Event") // < Xcode error...
}
错误:键入“SharedFunctions?”没有成员“createEvent”
我还尝试了以下错误:
weak var delegate = SharedFunctions? // < Xcode error...
错误:“弱”只能应用于类和类绑定的协议类型,而不是“SharedFunctions?.Type”。
我想要做的是,从我的SecondViewController类中触发createEvent()函数,这是我的FirstViewController类中的函数。
发布于 2019-09-19 02:03:20
试一试
class SecondViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
//Need change here
var sharedFunctions : SharedFunctions? // < Xcode error...
@IBAction func postChatMessageAction(_ sender: Any) {
self.sharedFunctions.createEvent(event: event, text: "New Event")
}
...
}
发布于 2019-09-19 02:02:21
SharedFunctions声明语法是错误的。它应该是
weak var delegate: SharedFunctions?
发布于 2019-09-19 02:24:03
首先,在func
的扩展中缺少一个FirstViewController
关键字。
extension FirstViewController: SharedFunctions {
func createEvent(event: Event, text: String) {
...
}
}
除此之外,sharedFunctions
属性在SecondViewController
中的声明是错误的。尝试以下几点:
class SecondViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var sharedFunctions: SharedFunctions?
@IBAction func postChatMessageAction(_ sender: Any) {
self.sharedFunctions.createEvent(event: event, text: "New Event")
}
...
}
希望这能有所帮助。
https://stackoverflow.com/questions/58008450
复制相似问题