无法从字符串中删除可选Word?

时间:2016-11-08 09:03:58

标签: ios swift

无法删除可选字

代码:

let questionidstr : String! = fetchResults.valueForKey("questionID").objectAtIndex(Counter) as? String

输出:  可选(16)

所需输出:  16

1 个答案:

答案 0 :(得分:0)

您正在使用as? String将其转换为可选字符串,因此可以选择输出。请尝试以下方法:

if let questionidstr = fetchResults.valueForKey("questionID").objectAtIndex(Counter) as? String {
    // Here, questionidstr will have a non optional value, but if it is nil the runtime will never get here.
}

你可以强行打开字符串,不需要条件绑定,但请注意这是不安全的,我不鼓励它采用上述方法:

let questionidstr = fetchResults.valueForKey("questionID").objectAtIndex(Counter) as! String