ObjC to Swift conversion of NSDictionary to NSObject : AnyObject

半城伤御伤魂 提交于 2019-12-06 09:50:56

问题


I'm implementing Segment.com's iOS library with Swift and all is working great, just stuck on the code conversion below of the identify method:

ref: https://segment.com/docs/libraries/ios/#identify

[[SEGAnalytics sharedAnalytics] identify:@"userId"
                                traits:@{ @"email": @"em@il.com" }];

where traits is an NSDictionary *, optional

Xcode tells me (typeahead hints) that in Swift it translates to:

SEGAnalytics.sharedAnalytics().identify(userId: String!, traits: [NSObject : AnyObject]!)

As a new Swift developer I'm struggling to get the syntax right to send an object into traits. How can I send in my set of predefined dictionary of optionals into the traits section? Something like: {email : email!, name : fullName!}


回答1:


Please try to use like this

var traitsDic : NSDictionary! = [ "email": "em@il.com", "firstName" : "Name" ]
var traits = traitsDic as Dictionary<String, AnyObject>

SEGAnalytics.sharedAnalytics().identify(userId: String!, traits: traits)


来源:https://stackoverflow.com/questions/29271230/objc-to-swift-conversion-of-nsdictionary-to-nsobject-anyobject

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!