问题
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