[英]Objective-C completion block in Swift errors
I'm trying to run a method from an Objective-C class with a completion block in a Swift class but I'm having some troubles. 我正在尝试从Objective-C类运行一个方法,并在Swift类中使用完成块,但遇到了一些麻烦。
Obj-C code: 对象代码:
typedef void(^completionBlock)(NSDictionary *);
+ (void)getVersionsFromAPI:(completionBlock)sendData
{
NSDictionary *dict = [[NSDictionary alloc] init];
// Do stuff
sendData(dict);
}
Swift code: SWIFT代码:
API.getVersionsFromAPI { (ver : NSDictionary) -> Void in
self.version = ver.mutableCopy() as NSMutableDictionary;
}
I'm getting an error that says '[NSObject : AnyObject]!' is not a subtype of 'NSDictionary'
我收到一个错误消息
'[NSObject : AnyObject]!' is not a subtype of 'NSDictionary'
'[NSObject : AnyObject]!' is not a subtype of 'NSDictionary'
on the first line. '[NSObject : AnyObject]!' is not a subtype of 'NSDictionary'
第一行'[NSObject : AnyObject]!' is not a subtype of 'NSDictionary'
。
I presume that the version
property is an optional NSMutableDictionary
: 我认为
version
属性是可选的NSMutableDictionary
:
var version: NSMutableDictionary?
If that's correct, than you should fix your code as follows: 如果正确,那么您应该按以下步骤修复代码:
API.getVersionsFromAPI { (ver: [NSObject: AnyObject]?) in
if let ver = ver {
self.version = NSMutableDictionary(dictionary: ver)
}
}
I've successfully compiled this code in Xcode 6.1.1 我已经在Xcode 6.1.1中成功编译了此代码
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.