简体   繁体   中英

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.

Obj-C code:

typedef void(^completionBlock)(NSDictionary *);

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

Swift code:

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' on the first line.

I presume that the version property is an optional 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM