0

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

2
  • Have you tried using ver : NSDictionary! or ver : NSDictionary?? Commented Dec 19, 2014 at 15:16
  • Yep, that then says '[NSObject : AnyObject]!' is not identical to 'NSDictionary' when using ! or the first error with ?. Commented Dec 19, 2014 at 15:18

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.