【问题标题】:How to Call a function having handler block in objective C from swift如何从swift调用目标C中具有处理程序块的函数
【发布时间】:2015-06-10 09:04:42
【问题描述】:

谁能告诉我如何从swift调用objective c类中定义的“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

【问题讨论】:

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


    【解决方案1】:

    假设你在 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
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多