ios - 如何在 Object C 中使用 block 调用方法?

标签 ios objective-c

我已将方法声明为:

- (void) authorizeUser:(OauthObject *) user withUsername: (NSString *) username withPassword: (NSString *) password completionHandler:(void (^)(NSData *data, NSURLResponse *response, NSError *error)) CallCompletion;

我尝试调用此方法,如下所示:

[[ManagerServerRequest sharedManagerServerRequest] authorizeUser:(OauthObject *) withUsername:self.login.text withPassword:self.login.text completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        // TODO
    }]

其中 ManagerServerRequest sharedManagerServerRequest 是单例对象。

我收到错误: 使用未声明的标识符 WithUsername

如何正确调用该方法?

评论: 我通过oauthObj:

OauthObject* oauthObj = [[OauthObject alloc] init];

[[ManagerServerRequest sharedManagerServerRequest]
 authorizeUser: oauthObj and:
 withUsername:self.login.text
 withPassword:self.login.text
 completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
     // TODO
 }]

我可能会出错

最佳答案

该问题与 block 语法无关。在你的代码中

[[ManagerServerRequest sharedManagerServerRequest] 
    authorizeUser:(OauthObject *) 
    withUsername:self.login.text 
    withPassword:self.login.text 
    completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        // TODO
}]

您缺少 authorizeUser: 的实际参数,将 (OauthObject *) 替换为实际对象即可。

关于ios - 如何在 Object C 中使用 block 调用方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34722984/

相关文章:

Objective-C:我应该声明私有(private)方法吗?

ios - setBackButtonBackgroundImage 时应用程序崩溃

objective-c - 使用 NSFetchedResultsController 将谓词从核心数据模型提取到各个部分中

ios - UIWebSelectSinglePicker 崩溃

objective-c - 获取任意类的类方法列表

ios - 如何设置 UISegmentedControl 框架?

IOS 在 facebook 上发布带有建议文本的照片

ios - RMStore 恢复应用内购买

iphone - 如何在iOS模拟器上查看通知中心?

iphone - 将一个字符串拆分成不同的字符串