简体   繁体   中英

NSDictionary vs [String:AnyObject] in Swift

I updated an external Swift library, and one of the methods has changed face from:

public convenience method(data:NSDictionary?) { ... }

to:

public convenience method(data:[String: AnyObject]?) { ... }
  1. What does this mean?

  2. Initially, with the old, @{@"optionA":@YES} was passed as data argument. But now with the new I need to pass two items. I was thinking to just write @{@"optionA":@YES,@"optionB":@NO} , is that correct?

  1. It just means that the parameter is a swift dictionary now and no Objective-C NSDictionary anymore.

  2. To pass data, just use square brackets instead of curly brackets and remove the "@" (in swift)

["optionA":true, "optionB": false]

If you are calling the method from Objective-C, the method parameter should be exposed as NSDictionary and you were right with @{@"optionA":@YES,"optionB":@NO} but missed a @-sign before optionB

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