Reference to a protocol inside a class throughs the following error: Expected member name or constructor call after type name
Reference to a protocol inside a class throughs the following error: Expected member name or constructor call after type name
这是我目前的情况:
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")
}
...
}
Error: Expected member name or constructor call after type name
当我更改我的代码时 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...
}
Error: Type 'SharedFunctions?' has no member 'createEvent'
我也尝试了以下错误:
weak var delegate = SharedFunctions? // < Xcode error...
Error: 'weak' may only be applied to class and class-bound protocol types, not 'SharedFunctions?.Type'
我想做的是,从我的 SecondViewController class 我想触发我的 FirstViewController class.
中的函数 createEvent()
您的 SharedFunctions 声明语法错误。应该是
weak var delegate: SharedFunctions?
尝试
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")
}
...
}
首先,FirstViewController
的扩展中缺少一个 func
关键字。尝试像这样实现 createEvent(event:text:)
函数:
extension FirstViewController: SharedFunctions {
func createEvent(event: Event, text: String) {
...
}
}
除此之外,SecondViewController
中sharedFunctions
属性的声明是错误的。请尝试以下操作:
class SecondViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var sharedFunctions: SharedFunctions?
@IBAction func postChatMessageAction(_ sender: Any) {
self.sharedFunctions.createEvent(event: event, text: "New Event")
}
...
}
希望对您有所帮助。
这是我目前的情况:
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")
}
...
}
Error: Expected member name or constructor call after type name
当我更改我的代码时 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...
}
Error: Type 'SharedFunctions?' has no member 'createEvent'
我也尝试了以下错误:
weak var delegate = SharedFunctions? // < Xcode error...
Error: 'weak' may only be applied to class and class-bound protocol types, not 'SharedFunctions?.Type'
我想做的是,从我的 SecondViewController class 我想触发我的 FirstViewController class.
中的函数 createEvent()您的 SharedFunctions 声明语法错误。应该是
weak var delegate: SharedFunctions?
尝试
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")
}
...
}
首先,FirstViewController
的扩展中缺少一个 func
关键字。尝试像这样实现 createEvent(event:text:)
函数:
extension FirstViewController: SharedFunctions {
func createEvent(event: Event, text: String) {
...
}
}
除此之外,SecondViewController
中sharedFunctions
属性的声明是错误的。请尝试以下操作:
class SecondViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var sharedFunctions: SharedFunctions?
@IBAction func postChatMessageAction(_ sender: Any) {
self.sharedFunctions.createEvent(event: event, text: "New Event")
}
...
}
希望对您有所帮助。