【发布时间】:2014-10-13 01:13:15
【问题描述】:
我有一个来自 UILocalNotification 的 userInfo 字典。使用隐式展开时是否有一种简单的方法来获取字符串值?
if let s = userInfo?["ID"]
给我一个 AnyObject,我必须将其转换为字符串。
if let s = userInfo?["ID"] as String
给我一个关于 StringLiteralConvertable 的错误
只是不想为了得到一个字符串而声明两个变量——一个用于展开的文字,另一个用于转换后的字符串。
编辑
这是我的方法。这也不起作用 - 我在 if 语句中得到 (NSObject, AnyObject) is not convertible to 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" )
}
}
我的问题中没有,但除了让这种方式工作之外,我还想真正拥有
if let s = notification.userInfo?["ID"] as String
【问题讨论】:
标签: dictionary swift optional