NS_REFINED_FOR_SWIFT和返回值 [英] NS_REFINED_FOR_SWIFT and return value

查看:199
本文介绍了NS_REFINED_FOR_SWIFT和返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Swift的新手,我开始探索一些与Objective-C桥接的功能.

I'm new with Swift and I begin exploring some feature for bridging with Objective-C.

目前,我有一个使用NSError引用的方法是

Currently I have a method with NSError reference which is:

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

现在,我可以在Swift中访问该方法以进行一些改进,但是返回值丢失了. Swift生成的方法是:

Now I can access the method in Swift for some refinements, but the return value is lost. The generated method for Swift is:

open func __verifyPersonalization() throws

使用do catch可以正确处理错误,但是返回值似乎丢失了.

The error is correctly handled with the do catch but the return value seems to be lost.

我的NS_REFINED_FOR_SWIFT宏缺少任何东西吗?

Any missing thing for my NS_REFINED_FOR_SWIFT macro?

推荐答案

这与NS_REFINED_FOR_SWIFT宏无关. Objective-C 方法

This is unrelated to the NS_REFINED_FOR_SWIFT macro. The Objective-C method

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

导入为Swift,

open func verifyPersonalization() throws

NS_REFINED_FOR_SWIFT宏的唯一作用是前置 强调Swift方法名称

and the only effect of the NS_REFINED_FOR_SWIFT macro is to prepend underscores to the Swift method name

open func __verifyPersonalization() throws

允许在扩展中提供精致的Swift接口,同时保持原始实现可从精致的接口调用 (请参阅 Swift和Objective中的精炼Objective-C声明" -C在同一项目中).

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导入程序假定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:

重要:方法的返回值指示成功或失败. 尽管在Cocoa错误中间接返回错误对象的Cocoa方法 如果方法指示失败,则域将保证返回此类对象 通过直接返回nil或NO,您应该始终检查返回值 尝试对NSError对象执行任何操作之前,该值为nil或NO.

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.

Objective-C的典型用法是

A typical usage in Objective-C is

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

Swift方法在​​失败时引发错误,因此不需要 布尔返回值:

The Swift method throws an error on failure, so there is no need for a boolean return value:

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


如果Objective-C方法确实通过退出指示失败 错误参数中的非空错误(而不是返回false,这是通常的可可模式),则可以指示 通过添加属性:


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:

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

导入为Swift的

open func verifyPersonalization() throws -> Bool

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

这篇关于NS_REFINED_FOR_SWIFT和返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆