ObjC to Swift将NSDictionary转换为NSObject:AnyObject [英] ObjC to Swift conversion of NSDictionary to NSObject : AnyObject
问题描述
我正在使用Swift实现Segment.com的iOS库,而且一切都很好,只是坚持识别
方法下面的代码转换:
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:
参考: https://segment.com/docs/ libraries / ios /#identify
[[SEGAnalytics sharedAnalytics] identify:@"userId"
traits:@{ @"email": @"em@il.com" }];
其中 traits
是 NSDictionary *,可选
Xcode告诉我(提前输入提示)在Swift中它转换为:
Xcode tells me (typeahead hints) that in Swift it translates to:
SEGAnalytics.sharedAnalytics().identify(userId: String!, traits: [NSObject : AnyObject]!)
作为一个新的Swift开发人员,我正在努力使语法正确地将对象发送到特征中。如何将我的预定义选项字典集发送到特征部分?类似于: {email:email !, name:fullName!}
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!}
推荐答案
<请尝试像这样使用
var traitsDic : NSDictionary! = [ "email": "em@il.com", "firstName" : "Name" ]
var traits = traitsDic as Dictionary<String, AnyObject>
SEGAnalytics.sharedAnalytics().identify(userId: String!, traits: traits)
这篇关于ObjC to Swift将NSDictionary转换为NSObject:AnyObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!