1

I defined a class as follows

@interface TestModel : NSObject

- (BOOL)test1:(NSError * _Nullable * _Nullable)error;

- (int)test2:(NSError * _Nullable * _Nullable)error;


@end
    
@implementation TestModel

- (BOOL)test1:(NSError * _Nullable * _Nullable)error {
    return YES;
}

- (int)test2:(NSError * _Nullable * _Nullable)error {
    if (error) {
        *error = [NSError errorWithDomain:@"domain" code:0 userInfo:nil];        
    }
    return 9;
}

@end

I use them like this in Swift

@objcMembers
@objc public class Utils: NSObject {
    func test() {
        do {
            let value = try TestModel().test1()
            print("success value: \(value)")
        } catch  {
            print("failed error: \(error)")
        }
        
        var error: NSError?
        let value = TestModel().test2(&error)
        print("value>>>>>> \(value)")
    }
}

Then, I got result

success value: ()
value>>>>>> 9

There are two problems here, I don't really understand.

  1. Why does test1 need catch exceptions, but test2 does not?
  2. Why does test1 not return a value?
x_z
  • 460
  • 5
  • 18
  • 1
    See [How to convert Objective-C BOOL to Swift's Bool with NSError** as one of the parameter in return](https://stackoverflow.com/questions/46626571/how-to-convert-objective-c-bool-to-swifts-bool-with-nserror-as-one-of-the-par) – HangarRash Dec 08 '22 at 14:56

0 Answers0