[英]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
/ false
或throw
,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.