简体   繁体   English

抛出方法不能是@objc协议的成员,因为它返回类型'Bool'的值

[英]Throwing method cannot be a member of an @objc protocol because it returns a value of type 'Bool'

I get following error: 我收到以下错误:

Throwing method cannot be a member of an @objc protocol because it returns a value of type 'Bool'; return 'Void' or a type that bridges to an Objective-C class

When defining Swift protocol which also needs to be bridged to Objective-C: 定义Swift协议时,还需要桥接到Objective-C:

@objc public protocol Saving {

    func save() throws -> Bool
}

Is there an other way to define Swift method which can return Bool , potentially throw an error and be Objetive-C compatible? 有没有其他方法来定义可以返回Bool Swift方法,可能会抛出错误并且与Objetive-C兼容?

As indicated in the comments, the following in Swift: 如评论中所示,Swift中的以下内容:

func save() throws

will be translated into: 将被翻译成:

(BOOL)saveAndReturnError:(NSError **)error

in Objective-C. 在Objective-C中。 Which explains the limitation. 这解释了这个限制。

I know that in the save() example, it might not make much sense to return a Bool as well as throwing, but I disagree with the comment about it not making sense at all. 我知道在save()示例中,返回Bool以及抛出可能没有多大意义,但我不同意关于它的评论根本没有意义。 There might be other use cases where it makes sense. 可能存在其他有用的用例。 Fx. FX。 the inverse example; 反例; loading Bool 's by using an identifer. 通过使用标识符加载Bool Loading a Bool might return true / false or throw if loading fails, fx. 如果加载失败,加载Bool可能会返回true / falsethrow ,fx。 if the identifier was not found when trying to load. 如果在尝试加载时未找到标识符。

However unfortunately we cannot do this because of how Swift and Objective-C is being bridged. 但遗憾的是,由于Swift和Objective-C的桥接方式,我们无法做到这一点。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 方法不能是 @objc 协议的成员,因为它的结果类型不能在 Objective-C 中表示 - Method cannot be a member of an @objc protocol because its result type cannot be represented in Objective-C 使用字典时,方法不能为@objc协议的成员 - Method cannot be a member of an @objc protocol while using Dictionary Swift @objc 协议不能用作符合协议 'Equatable' 的类型,因为 'Equatable' 有静态要求 - Swift @objc protocol cannot be used as a type conforming to protocol 'Equatable' because 'Equatable' has static requirements 方法不能标记为@objc,因为其结果类型无法在Objective-C中表示 - Method cannot be marked @objc because its result type cannot be represented in Objective-C ObjC错误抛出函数返回()而不是BOOL - ObjC error throwing function returning () instead of BOOL 在@objc协议中定义带闭包的方法 - Define method with closure in an @objc protocol 成员“观察”不能用于协议类型的值; 改用通用约束 - Member 'observe' cannot be used on value of protocol type; use generic constraint instead 属性不能被标记为@objc,因为它的类型不能在 Objective-C 中表示 - Property cannot be marked @objc because its type cannot be represented in Objective-C 无法调用在Swift中使用int的ObjC方法,因为Int32无法转换为Int - Cannot call an ObjC method taking int in Swift because Int32 cannot be converted to Int objc:避免协议方法名称冲突? - objc: avoid protocol method name conflicts?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM