ObjC to Swift conversion of NSDictionary to NSObje

2019-04-13 20:58发布

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条回答
看我几分像从前
2楼-- · 2019-04-13 21:44

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)
查看更多
登录 后发表回答