简体   繁体   English

NS_REFINED_FOR_SWIFT并返回值

[英]NS_REFINED_FOR_SWIFT and return value

I'm new with Swift and I begin exploring some feature for bridging with Objective-C. 我是Swift的新手,我开始探索一些与Objective-C进行桥接的功能。

Currently I have a method with NSError reference which is: 目前我有一个NSError引用的方法,它是:

- (BOOL) verifyPersonalizationWithError:(NSError **) error NS_REFINED_FOR_SWIFT;

Now I can access the method in Swift for some refinements, but the return value is lost. 现在我可以在Swift中访问该方法进行一些改进,但返回值会丢失。 The generated method for Swift is: Swift生成的方法是:

open func __verifyPersonalization() throws

The error is correctly handled with the do catch but the return value seems to be lost. 使用do catch正确处理错误但返回值似乎丢失了。

Any missing thing for my NS_REFINED_FOR_SWIFT macro? 我的NS_REFINED_FOR_SWIFT宏有什么遗漏的东西?

This is unrelated to the NS_REFINED_FOR_SWIFT macro. 这与NS_REFINED_FOR_SWIFT宏无关。 The Objective-C method Objective-C方法

 - (BOOL) verifyPersonalizationWithError:(NSError **) error;

is imported to Swift as 被导入到Swift中

open func verifyPersonalization() throws

and the only effect of the NS_REFINED_FOR_SWIFT macro is to prepend underscores to the Swift method name 并且NS_REFINED_FOR_SWIFT宏的唯一作用是将下划线添加到Swift方法名称

open func __verifyPersonalization() throws

which allows to provide a refined Swift interface in an extension, while keeping the original implementation available to be called from the refined interface (see "Refining Objective-C Declarations" in Swift and Objective-C in the Same Project ). 它允许在扩展中提供精炼的Swift接口,同时保持原始实现可从精炼接口调用(参见同一项目中的Swift和Objective-C中的 “精炼Objective-C声明”)。

The Swift importer assumes that the boolean return value of the Objective-C method indicates success or failure, which is the common Cocoa pattern as documented in Using and Creating Error Objects : Swift导入器假定Objective-C方法的布尔返回值表示成功或失败,这是使用和创建错误对象中记录的常见Cocoa模式:

Important: Success or failure is indicated by the return value of the method. 重要说明:方法的返回值表示成功或失败。 Although Cocoa methods that indirectly return error objects in the Cocoa error domain are guaranteed to return such objects if the method indicates failure by directly returning nil or NO, you should always check that the return value is nil or NO before attempting to do anything with the NSError object. 虽然在Cocoa错误域中间接返回错误对象的Cocoa方法可以保证返回这样的对象,如果方法通过直接返回nil或NO来指示失败,你应该在尝试对之做任何事情之前检查返回值为nil或NO。 NSError对象。

A typical usage in Objective-C is Objective-C中的典型用法是

NSError *error;
if ([self verifyPersonalizationWithError:&error]) {
    NSLog(@"success");
} else {
    NSLog(@"failed: %@", error.localizedDescription);
}

The Swift method throws an error on failure, so there is no need for a boolean return value: Swift方法在​​失败时抛出错误,因此不需要布尔返回值:

do {
    try verifyPersonalization()
    print("success")
} catch {
    print("failed:", error.localizedDescription)
}

If the Objective-C method really indicates failure by leaving a non-null error in the error parameter (instead of returning false , which is the usual Cocoa pattern) then you can indicate that by adding a attribute: 如果Objective-C方法通过在error参数中留下非null错误(而不是返回false ,这是通常的Cocoa模式) 确实指示失败,那么您可以通过添加属性来指示:

- (BOOL) verifyPersonalizationWithError:(NSError **) error
    __attribute__((swift_error(nonnull_error)));

which is imported to Swift as 这是导入Swift的

open func verifyPersonalization() throws -> Bool

The swift_error attribute is documented at https://github.com/apple/swift-clang/blob/383859a9c4b964af3d127b5cc8abd0a8f11dd164/include/clang/Basic/AttrDocs.td#L1800-L1819 swift_error属性记录在https://github.com/apple/swift-clang/blob/383859a9c4b964af3d127b5cc8abd0a8f11dd164/include/clang/Basic/AttrDocs.td#L1800-L1819

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM