无法在@objc属性的协议中使用自定义类? [英] Unable to use custom class in a protocol with @objc attribute?
问题描述
我正在尝试为JSON加载委托创建协议, JSONLoaderDelegate
。我的另一个类叫做$ code> JSONLoader ,它应该将事件分派给它的代理(实现 JSONLoaderDelegate
协议),如:
I am trying to create a protocol for JSON loading delegation, JSONLoaderDelegate
. My other class, called JSONLoader
, is supposed to dispatch events to its delegate (that implements the JSONLoaderDelegate
protocol) like:
self?.delegate?.jsonLoaderdidEndWithError(self!, error: JSONLoaderError.LoadError)
JSONLoader的实现并不重要(imho)。但是我似乎有执行协议的问题,这是代码:
The implementation of the JSONLoader is not that important (imho). However I seem to have problems to implement the protocol, this is the code:
@objc protocol JSONLoaderDelegate {
optional func jsonLoaderdidBeginLoading(jsonLoader: JSONLoader)
func jsonLoaderdidEndWithError(jsonLoader: JSONLoader, error: JSONLoader.JSONLoaderError)
func jsonLoaderDidEndWithSuccess(jsonLoader: JSONLoader)
}
这看起来很简单,但我收到一个错误:
This looks pretty straightforward to me but I am getting an error:
方法不能被标记为@objc,因为参数的类型不能
在Objective-C中表示。
method cannot be marked @objc because the type of the parameter cannot be represented in Objective-C.
指向所有三个功能。
显然,如果我删除 @objc
属性我不能使用该功能的可选
。我真的希望保持 jsonLoaderdidBeginLoading
作为可选的。任何想法/解决方法?谢谢!
Obviously, if I remove the @objc
attribute I cannot use the optional
for the function. I would really like to keep jsonLoaderdidBeginLoading
as optional tho. Any ideas / ways to solve this? Thank you!
推荐答案
3种方法的共同点是 JSONLoader
参数,这就是我认为阻止你桥接协议。为了解决这个问题,你必须使它objc兼容。
What the 3 methods have in common is the JSONLoader
parameter, and that's what I think prevents you from bridging the protocol. In order to solve the problem you have to make it objc compatible.
这篇关于无法在@objc属性的协议中使用自定义类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!