khabiroff
khabiroff

Reputation: 240

Swift changes the type of parameter of a block, which is also the parameter of Objective-C method

I have an Objective-C class that has methods with block parameters:

+ (void)getCurrentUserInfoWithToken:(NSString*)token completionHandler:(void (^)(NSDictionary* userData))handler

As you can see, the block has NSDictionary parameter. But when I try to cast this method in swift, it gives an error: "'[NSObject : AnyObject]' is not identical to 'NSDictionary'". Here's my Swift code:

ClockfaceAPI.getCurrentUserInfoWithToken(token, completionHandler: {
            (userData : NSDictionary!) in
            // block implementation goes here
})

And I have no idea how to solve it =/

Upvotes: 0

Views: 582

Answers (1)

DPlusV
DPlusV

Reputation: 14326

This makes sense, as your NSDictionary* gets interpreted as [NSObject:AnyObject]

You don't have to specify the type. E.g. just saying userData without : NSDictionary! will work fine. Swift will automatically infer the type based on the declaration.

ClockFaceAPI.getCurrentUserInfoWithToken(token, completionHandler: { (dict) -> Void in
    for (k, v) in dict {
        println(k)
        println(v)
    }
})

Upvotes: 1

Related Questions