Unable to use custom class in a protocol with @objc attribute?(无法在具有@objc 属性的协议中使用自定义类?)
问题描述
我正在尝试为 JSON 加载委托创建一个协议,JSONLoaderDelegate
.我的另一个类,称为 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 的实现并不那么重要(恕我直言).但是我似乎在实现协议时遇到了问题,这是代码:
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
属性,我就不能将 optional
用于该函数.我真的很想将 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 属性的协议中使用自定义类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:无法在具有@objc 属性的协议中使用自定义类?
基础教程推荐
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01