无法删除可选字
代码:
let questionidstr : String! = fetchResults.valueForKey("questionID").objectAtIndex(Counter) as? String
输出: 可选(16)
所需输出: 16
答案 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