I need to call an Objective C method from Swift -
i have interface in objective c trying call swift , not seem fall completion code. not sure missing!
@interface wplogin : nsobject /*! * @brief logs application returning success or failure error object. * if login successful credential automatically stored * * @param serverurlstring address login * @param username username login * @param password password user * @param completion completion block 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 swift looks 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 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"); }];
the problem in swift version wplogin object local variable (var loginauth). therefore dies before has chance do anything. make property, in 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 take action over time - going out on internet, logging in, calling completion handler - , cannot if dies instantly, in swift code. must persist on significant time.
Comments
Post a Comment