ObjC to Swift将NSDictionary转换为NSObject:AnyObject [英] ObjC to Swift conversion of NSDictionary to NSObject : AnyObject

查看:778
本文介绍了ObjC to Swift将NSDictionary转换为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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆