嗨,我一直在迅速解决错误,因为我无法匹配Objective-C方法的参数

    SimpleAuth.authorize("instagram",

       options: ["scope" : "likes"], completion: {

          (responseObject : NSDictionary!, error : NSError!) -> Void in

          self.accessToken = responseObject["credentials"]["token"]

          ......

    })

//错误调用中有额外的参数“options”

声明为:
  + (void)authorize:(NSString *)provider options:(NSDictionary *)options completion:(SimpleAuthRequestHandler)completion;

代码完成是:
  SimpleAuth.authorize(<#provider: String!#>, options: <#[NSObject : AnyObject]!#>, completion: <#SimpleAuthRequestHandler!##(AnyObject!, NSError!) -> Void#>)

我尝试过类型转换,向下转换,声明为单独的变量,但仍然无法使其正常工作。

任何想法,将不胜感激

最佳答案

这是您对完成区块的定义。 SimpleAuthRequestHandler定义为:

typedef void (^SimpleAuthRequestHandler) (id responseObject, NSError *error);

但是您的完成阻止/结束操作定义为:
(responseObject : NSDictionary!, error : NSError!) -> Void in

您不能将类型从id(在Swift中为AnyObject)更改为NSDictionary!,而无需显式转换它。您的通话应如下所示:
SimpleAuth.authorize("instagram", options: ["scope" : "likes"], completion: {
    (responseObject : AnyObject!, error : NSError!) -> Void in

    /* ... */
})

然后,可以使用强制转换将responseObject变成NSDictionary:
var response = responseObject as NSDictionary

关于ios - Swift错误-调用SimpleAuth时需要额外的参数'option',我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26287825/

10-11 00:58