0

Some background: I'm building an app in react native that uses an Objective C library written about 6 or 7 years ago, maybe older. I'm writing swift code that has been set up to send callbacks to the react native application in JS. I have this function that I'm trying to use:

token = service.getUserToken(server,
                port:  P2PFunctions.tls_port,
                appId: P2PFunctions.appID,
                appSecret: P2PFunctions.appSecret,
                phone: P2PFunctions.phone,
                token: nil, errcode: errCode, errmsg: nil);
        callback(["\(token!)"]);

And this is its definition:

- (NSInteger)getUserToken:(NSString*)ip_In port:(NSInteger)port_In appId:
(NSString*)appId_In appSecret:(NSString*)appSecret_In phone:
(NSString*)phoneNum_In token:(NSString**)accessTok_Out errcode:
(NSString**)strErrCode_Out errmsg:(NSString**)errMsg_Out;

These are the types I'm using (EDIT: I changed them from private to public, and they still are not being recognized):

Static Variables

The problem is, I'm getting nil back from the function. I believe I'm getting an HTTP response that is empty, and I notice that inside the debugger when I step to the Objective C function, I see nil for all my parameters inside of the Objective C function. I think... it is that I'm not passing the correct type. Or my Swift parameters are not visible in Objective C's memory space. If it is expecting an (NSString *), should I be passing a String?

How do I pass the correct types from Swift to Objective C? What would I change in my function call? Are my parameter types okay? I cannot edit the original Objective C library. They share a common memory space for all variables in the entire program, right?

Thank you so much!

4
  • 1
    Swift itself is type safe, so unless you use Any/id you cannot pass incorrect types from Swift to Obj-C. If you call the same function from Obj-C, does it work fine? Commented May 29, 2020 at 17:05
  • There is no special trick for passing parameters from Swift to Objective-C. If your code compiles it should work. One thing I find suspicious is that you say the method is returning nil but the definition says it returns NSInteger which Swift should automatically convert to its Int type. This method cannot return nil to Swift. So what are you talking about? Commented May 29, 2020 at 17:27
  • That was incorrect, it returns an error code of -1, a response variable inside that function returns nil with those parameters as values, sorry about that. Commented May 29, 2020 at 17:33
  • @DávidPásztor I created a function called doTask where I initialize the variables inside the function. I call it from Swift, and the debugger says they are all nil inside there too, which doesn't make any sense. I'm going to try to investigate this further. Commented May 29, 2020 at 17:36

2 Answers 2

2

I just ran this successfully:

// the objc part
@interface Test : NSObject

- (NSInteger)getUserToken:(NSString*)ip_In
                     port:(NSInteger)port_In
                    appId:(NSString*)appId_In
                appSecret:(NSString*)appSecret_In
                    phone:(NSString*)phoneNum_In
                    token:(NSString* _Nonnull * _Nonnull)accessTok_Out
                  errcode:(NSString* _Nonnull * _Nonnull)strErrCode_Out
                   errmsg:(NSString* _Nonnull * _Nonnull)errMsg_Out;

@end

@implementation Test

- (NSInteger)getUserToken:(NSString*)ip_In
                     port:(NSInteger)port_In
                    appId:(NSString*)appId_In
                appSecret:(NSString*)appSecret_In
                    phone:(NSString*)phoneNum_In
                    token:(NSString**)accessTok_Out
                  errcode:(NSString**)strErrCode_Out
                   errmsg:(NSString**)errMsg_Out {

    *accessTok_Out = @"Token";
    *strErrCode_Out = @"OK";
    *errMsg_Out = @"msg";

    return 42;
}

@end

// and the swift part
let t = Test()
var token: NSString = "t"
var errcode: NSString = "c"
var errmsg: NSString = "m"
let result = t.getUserToken("ip", port: 1, 
   appId: "2", appSecret: "3", phone: "4", 
   token: &token, errcode: &errcode, errmsg: &errmsg)
print(result)

and it works as expected.

Maybe this gives you a hint as to what's different in your situation.

Sign up to request clarification or add additional context in comments.

Comments

0

So, after spending a week or so on this problem, I realize that it was not that the parameters are not actually being passed, but that the debugger is just not displaying the values of those parameters, at least on the main thread. Because I was getting the error code, I thought that something had to be wrong with the way I called the function - but actually, the function call is fine, the variables just didn't appear in the debugger for some reason:

Varables are nil in the debugger, but they are in fact being passed correctly.

Variables above appear to be nil - but in fact, they do have values.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.