Swift错误中的Objective-C完成块

时间:2014-12-19 15:14:22

标签: ios objective-c swift nsdictionary nsobject

我试图在Swift类中使用完成块从Objective-C类运行一个方法,但是我遇到了一些麻烦。

Obj-C代码:

typedef void(^completionBlock)(NSDictionary *);

+ (void)getVersionsFromAPI:(completionBlock)sendData
{
  NSDictionary *dict = [[NSDictionary alloc] init];
  // Do stuff
  sendData(dict);
}

Swift代码:

API.getVersionsFromAPI { (ver : NSDictionary) -> Void in
    self.version = ver.mutableCopy() as NSMutableDictionary;
}

我在第一行收到'[NSObject : AnyObject]!' is not a subtype of 'NSDictionary'的错误。

1 个答案:

答案 0 :(得分:2)

我认为version属性是可选的NSMutableDictionary

var version: NSMutableDictionary?

如果这是正确的,那么您应按如下方式修改代码:

API.getVersionsFromAPI { (ver: [NSObject: AnyObject]?) in
    if let ver = ver {
        self.version = NSMutableDictionary(dictionary: ver)
    }
}

我已经在Xcode 6.1.1

中成功编译了这段代码