简体   繁体   English

如何编写带有throws语句的swift 3方法和可以在Objective C中使用的返回值?

[英]How to write swift 3 method with throws statement and return value that can be used in Objective C?

Below Swift 3 method is translated into Objective C like: 下面的Swift 3方法被翻译成Objective C,如:

func doSomething(param: String) throws // Swift 3

- (BOOL)doSomething:(NSString * _Nonnull)param error:(NSError * _Nullable * _Nullable)error; // Translated Objective C

Then, how can I write a method with both throws and a return type? 那么,如何编写一个包含throws和return类型的方法?

func doSomething(param: String) throws -> Int // Swift 3

// Error: Never translated into Objective C

I know flow should not be handled by NSError object. 我知道流不应该由NSError对象处理。 It just contains information about an error. 它只包含有关错误的信息。 And that's why there's a BOOL return type to let us know the invocation is succeeded without any problem. 这就是为什么有一个BOOL返回类型让我们知道调用成功没有任何问题。

Then how can I handle a method with both throws statement and return type? 那我怎么能用throws语句和返回类型处理一个方法? Is there a standard way to handle this? 有没有一种标准的方法来处理这个问题?

Thank you in advance. 先感谢您。

The standard way of reporting success or failure in Objective-C is to return the boolean value NO or a nil object pointer, as documented in Using and Creating Error Objects : 在Objective-C中报告成功或失败的标准方法是返回布尔值NOnil对象指针,如使用和创建错误对象中所述

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对象。

So you can return a instance of an object 所以你可以返回一个对象的实例

func doSomething(param: String) throws -> NSNumber

which is translated to 这被翻译成

- (NSNumber * _Nullable)doSomethingWithParam:(NSString * _Nonnull)param error:(NSError * _Nullable * _Nullable)error;

and returns nil if an error is thrown, or return a boolean and pass other values back by reference 如果抛出错误则返回nil ,或返回布尔值并通过引用返回其他值

func doSomething(param: String, value: UnsafeMutablePointer<Int>) throws

which is mapped to 映射到

- (BOOL)doSomethingWithParam:(NSString * _Nonnull)param value:(NSInteger * _Nonnull)value error:(NSError * _Nullable * _Nullable)error;

试试这个:

- (int) doSomething:(NString *)param error:(NSError * _Nullable *)error;

There is another way to handle this if you can modify the throwable object. 如果可以修改throwable对象,还有另一种方法可以解决这个问题。

If you can put up an enum for the errors, and annotate the error enum with @objc than the automatic bridge header will convert it to NSError like mentioned here before. 如果您可以为错误添加枚举,并使用@objc注释错误枚举,则自动桥接头将其转换为NSError,如前所述。

For example: 例如:

@objc enum MyError:Int, Error{
    case AnError
    case AnotherError
}

And your throwing swift method: 而你的投掷快速方法:

public class MyClass:NSObject{

    public func throwAnError() throws {
        throw MyError.AnotherError
    }

    public func callMe(){
        print("Someone called!")
    }

}

Will be converted by the auto bridging process to: 将通过自动桥接过程转换为:

SWIFT_CLASS("_TtC13ErrorHandling7MyClass")
@interface MyClass : NSObject
- (BOOL)throwAnErrorAndReturnError:(NSError * __nullable * __null_unspecified)error;
- (void)callMe;
- (nonnull instancetype)init OBJC_DESIGNATED_INITIALIZER;
@end

Taken from the bottom of this excellent blog post which is relevant to this question. 摘自这篇与此问题相关的优秀博客文章的底部。

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

相关问题 我们如何编写用户定义的方法是带有完成处理程序的Objective C / Swift - How can we write a user defined method is Objective C / Swift with completion handler 如何在 Swift 语言中调用 Objective C 方法(即返回协议) - How to call Objective C method(which is the return protocol) in Swift Language Swift 编写一个带有返回值的 async/await 方法 - Swift write an async/await method with return value Swift中使用的Objective-C框架——方法错误的歧义使用 - Objective-C framework used in Swift - ambiguous use of method error 如何从目标C调用Swift方法 - How to call swift method from objective c 如何更快地编写类似于目标C的getter方法,并提供更好的方法 - How write getter method in swift same like objective C, with better ways 如何在方法中使用此值 - 目标C. - How can I work with this value in a method - Objective C 如何替换SWIFT中Objective-C中曾经拥有的#define宏? - How can I replace a #define macro I used to have in Objective-C in SWIFT? 如何使用 swift Viewcontroller 通过编写代码打开 objective-c Viewcontrollerv 页面? - How can I use swift Viewcontroller through write coding to open the objective-c Viewcontrollerv page? Swift值类型和Swift中Objective-C NSObject子类如何设计协议? - How to design protocols that can be applied to both Swift value types and Objective-C NSObject subclasses in Swift?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM