0

I have this function in my Objective-c framework:

- (BOOL)startWithAccount(NSString*)account andUser:(NSString*)user error:(NSError**)error

And I want to call it from Swift code, so I create a class and call it:

self.loginState.start(withAccount: ACCOUNT_KEY, andUser: USER_ID)

But the autoComplete does not give me the option to call the error parameter and the return value is not Bool:

var result: ()? = try? self.loginState.start(withAccount: ACCOUNT_KEY, andUser: USER_ID)

and the result value is nil. any idea what is the problem?

YosiFZ
  • 7,792
  • 21
  • 114
  • 221
  • The result value being nil means that an error has occurred. There is no problem here. This is just how Objective-C APIs translate to Swift. – Sweeper Feb 28 '23 at 08:53
  • Please learn about how [error handling works in Swift](https://docs.swift.org/swift-book/documentation/the-swift-programming-language/errorhandling/) first if you haven't already. – Sweeper Feb 28 '23 at 08:56
  • 2
    Don't `try ?`. Use `try`. Then you can `catch` the error if one is thrown – Paulw11 Feb 28 '23 at 09:09
  • Does this answer your question? [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 Feb 28 '23 at 18:10
  • Does this answer your question? [Throwing method cannot be an implementation of an @objc requirement because it returns a value of type \`Bool\`](https://stackoverflow.com/questions/75114968/throwing-method-cannot-be-an-implementation-of-an-objc-requirement-because-it-r) – The Dreams Wind Mar 01 '23 at 05:38

1 Answers1

-1

Try this:

do {
    var result = try self.loginState.start(withAccount: ACCOUNT_KEY, andUser: USER_ID)
    // continue with result
} catch {
    print("You have \(error) here")
}
Predrag Samardzic
  • 2,661
  • 15
  • 18