简体   繁体   中英

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 =/

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

You don't have to specify the type. Eg 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)
    }
})

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