【问题标题】:Objective C to Swift Methods call?目标 C 到 Swift 方法调用?
【发布时间】:2014-07-23 00:41:24
【问题描述】:

这是我需要转换为快速调用的目标 c 方法调用。

它抱怨“找不到接受提供的参数的 logInWithUsernameInBackground 的重载。

我做错了什么?

Objective C 方法声明

typedef void (^PFUserResultBlock)(PFUser *user, NSError *error);

+ (void)logInWithUsernameInBackground:(NSString *)username
                             password:(NSString *)password
                                block:(PFUserResultBlock)block;

目标 C 调用

[User logInWithUsernameInBackground:@""
                            password:@""
                               block:^(PFUser *user, NSError *error) {
     NSLog(@"test");
 }];

斯威夫特

User.logInWithUsernameInBackground("", 
  password: "", 
     block: { (user: PFUser?, error: NSError) -> Void in
       NSLog("test")
    }
)

【问题讨论】:

  • 请在Objective-C中显示该方法的声明。重要的是,这不是你所说的那样。
  • { (user, error) in NSLog("test")}
  • 在我的问题中添加了方法定义
  • @BryanChen 你能把它作为答案提交给我吗?

标签: objective-c swift


【解决方案1】:

让编译器通过类型推断为您找出正确的类型

User.logInWithUsernameInBackground("", 
  password: "", 
     block: {
     (user, error) in
         NSLog("test")
    } 
)

【讨论】:

  • Objective c 用于自动完成块定义。 swift 中的关闭似乎不会发生这种情况
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-02
相关资源
最近更新 更多