如何从swift调用目标C中具有处理程序块的函数

时间:2015-06-10 09:04:42

标签: ios objective-c swift objective-c-blocks

请有人告诉我如何从swift中调用Objective c class中定义的“attemptLogin”登录函数

#import <Foundation/Foundation.h>

@interface LoginHandler : NSObject<NSURLConnectionDelegate,NSXMLParserDelegate>{

}

typedef void (^CustomCallback)(NSDictionary *responseObj);

-(void)attemptLogin:(NSString *)userName andPassword:(NSString *)password completionHandler:(CustomCallback) callback;
@end

,我在swift文件中调用

var loginHandler = LoginHandler();
        loginHandler.attemptLogin(userMob, andPassword: userPass){
            data in


        }

但是data以NSObject形式返回,但它应该是NSDictionary

1 个答案:

答案 0 :(得分:0)

假设您在Objective C中有阻止 -

 [[MyDataStore instance] askUserForMe:^(Person *person, NSError *error){
        // do things with myself that aren't strange
    }];

以下代码将是您从swift调用的时间 -

MyDataStore.instance().askUserForMe() {
    person, error in 
    // do things with myself that aren't strange
}