0

I have this interface in Objective C that I am trying to call from Swift and it does not seem fall into the completion code. Not sure what I am missing!

@interface WPLogin : NSObject /*! * @brief Logs into the application returning success or failure with an error object. * If login is successful the credential is automatically stored * * @param serverURLString Address to login to * @param username Username to login with * @param password Password for user * @param completion Completion block to be called after attempting login */ - (void)loginToURL:(NSString *)serverURLString withUsername:(NSString *)username password:(NSString *)password completion:(void (^)(WPLoginStatus success, NSError * error))completion; @end 

the calling function from Swift looks like this...

 var uid = "test" var pwd = "test" var url = "http://www.google.com" var loginAuth = WPLogin(); loginAuth.loginToURL(url, withUsername: uid, password: pwd, completion: { (status:WPLoginStatus, error:NSError!) -> Void in println("Inside Login") }) 

bridging file

#import <Foundation/Foundation.h> #import <UIKit/UIKit.h> #import "WPLogin.h" 

version that works in Objective C

if (!self.login) { self.login = [WPLogin new]; } [self.login loginToURL:url withUsername:uid password:pwd completion:^(WPLoginStatus status, NSError *error) { NSLog(@"complete"); }]; 
8
  • 1
    Is #import "WPLogin.h" in your bridging header (if you don't have a bridging header you'll need one). Commented Apr 24, 2015 at 14:56
  • Sorry, forgot to include that. I do have it and it is listed. Code completion shows up and project compiles. There aren't any run time errors either. Just hits that line and keeps on going. Never falls into the completion code to println() Commented Apr 24, 2015 at 15:00
  • It must be the case that the Objective-C method is not calling the completion method. The call looks fine. Commented Apr 24, 2015 at 15:03
  • Ok. Thanks. It works from Objective C but was trying to convert to Swift and can't get that to run. Commented Apr 24, 2015 at 15:07
  • "hits that line and keeps on going" correct, because the completion code is asynchronous Commented Apr 24, 2015 at 15:07

1 Answer 1

1

The problem is that in your Swift version the WPLogin object is a local variable (var loginAuth). Therefore it dies before it has a chance to do anything. Make it a property, as in your Objective-C version (self.login):

class MyClass { var login : WPLogin = WPLogin() func myMethod () { var uid = "test" var pwd = "test" var url = "http://www.google.com" self.login.loginToURL(url, withUsername: uid, password: pwd, completion: { (status:WPLoginStatus, error:NSError!) -> Void in println("Inside Login") }) } } 

This object needs to take action over time - going out on the Internet, logging in, calling your completion handler - and it cannot do that if it dies instantly, as it does in your Swift code. It must persist over significant time.

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

2 Comments

On the lifetime of objects assigned to different kinds of variables, see my book: apeth.com/swiftBook/ch03.html#_variable_scope_and_lifetime
Also it appears from your comments that you may need to learn what "asynchronous" means in general. See my discussion here: stackoverflow.com/a/27053243/341994

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.