大佬教程收集整理的这篇文章主要介绍了从userInfo Dictionary获取字符串,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
if let s = userInfo?["ID"]@H_450_8@
if let s = userInfo?["ID"] as String@H_450_8@给我一个关于StringLiteralConvertable的错误
只是不想声明两个变量来获取字符串 – 一个用于解包的文字和另一个用于转换字符串的var.
编辑
这是我的方法.这也不起作用 – 我得到(NSObject,AnyObject)在if语句中不能转换为String.
for notification in scheduledNotifications { // optional chainging let userInfo = notification.userInfo if let id = userInfo?[ "ID" ] as? String { println( "Id found: " + id ) } else { println( "ID not found" ) } }@H_450_8@我没有在我的问题中,但除了这种方式工作,我想真的有
if let s = notification.userInfo?["ID"] as String@H_450_8@
(注意:这适用于Xcode 6.1.对于Xcode 6.0,请参见下文)
if let s = userInfo?["ID"] as? String { // When we get here,we kNow "ID" is a valid key // and that the value is a String. }@H_450_8@此构造从userInfo安全地提取字符串:
>如果userInfo为nil,userInfo?[“ID”]由于可选链接而返回nil,条件转换返回String类型的变量?它的值为零.然后,可选绑定失败,并且未输入块.
>如果“ID”不是字典中的有效键,userInfo?[“ID”]返回nil,它会像前一种情况一样继续.
>如果值是另一种类型(如int),则条件转换为?将返回零,并像上述情况一样继续.
>最后,如果userInfo不是nil,并且“ID”是字典中的有效键,并且值的类型是String,则条件转换返回可选字符串String?包含字符串.可选绑定如果let然后解包String并将其分配给将具有String类型的s.对于Xcode 6.0,您还必须做一件事.您需要有条件地转换为NSString而不是String,因为NSString是一个对象类型而String不是.他们显然改进了Xcode 6.1中的处理,但对于Xcode 6.0,请执行以下操作:
if let s:string = userInfo?["ID"] as? NSString { // When we get here,we kNow "ID" is a valid key // and that the value is a String. }@H_450_8@
for notification in scheduledNotifications { if let id:string = notification.userInfo?["ID"] as? NSString { println( "Id found: " + id ) } else { println( "ID not found" ) } }@H_450_8@
以上是大佬教程为你收集整理的从userInfo Dictionary获取字符串全部内容,希望文章能够帮你解决从userInfo Dictionary获取字符串所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。