【问题标题】:Rubymotion and Objective C getter blocksRubymotion 和 Objective C getter 块
【发布时间】:2014-01-20 15:17:50
【问题描述】:

我对 RubyMotion 很陌生,而且我刚刚碰壁。

我正在尝试做一些 LinkedIn oauth 工作,我需要将以下内容转换为 RubyMotion

client = LIALinkedInHttpClient.clientForApplication(application, presentingViewController:nil)

client getAuthorizationCode:^(NSString * code) {
    [self.client getAccessToken:code success:^(NSDictionary *accessTokenData) {
        NSString *accessToken = [accessTokenData objectForKey:@"access_token"];
        [self.client getPath:[NSString stringWithFormat:@"https://api.linkedin.com/v1/people/~?oauth2_access_token=%@&format=json", accessToken] parameters:nil success:^(AFHTTPRequestOperation * operation, NSDictionary *result) {
            NSLog(@"current user %@", result);
        } failure:^(AFHTTPRequestOperation * operation, NSError *error) {
            NSLog(@"failed to fetch current user %@", error);
        }];
    } failure:^(NSError *error) {
        NSLog(@"Quering accessToken failed %@", error);
    }];
} cancel:^{
    NSLog(@"Authorization was cancelled by user");
} failure:^(NSError *error) {
    NSLog(@"Authorization failed %@", error);
}];

谁能指出我的写作方向?

【问题讨论】:

    标签: objective-c cocoapods rubymotion


    【解决方案1】:

    使用 Ruby 2.0 的 stabby lambda 的少打字方式如下所示:

    client.getAuthorizationCode -> (code) {
      NSLog "Success"
    }, cancel: ->
      NSLog "Auth was cancelled"
    }, failure: -> (error) {
      NSLog "Auth failed"
    }
    

    【讨论】:

    • 谢谢,我选择了这个作为答案,只是因为它是 Ruby 2.0。非常适合我。
    【解决方案2】:

    这里有一个关于如何在 RubyMotion 中使用 Objective-C 块的想法:

    client.getAuthorizationCode(lambda { |code|
    
    }, cancel: lambda { 
    
    }, failure: lambda { |error|
    
    })
    

    如果您愿意,我相信您可以使用 lambda 简写 ->Proc。有关更多信息,请参阅RubyMotion docs。他们演示了使用doend 来开始和结束块,但为此,我更喜欢大括号。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      相关资源
      最近更新 更多