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.
- Why does test1 need catch exceptions, but test2 does not?
- Why does test1 not return a value?