我从plists中的字典中加载一个值,但是当我将其打印到控制台时,它会打印:可选(星期一标题),而不是“星期一标题”.
打印时如何摆脱我的价值可选()?
var plistPath = NSBundle.mainBundle().pathForResource("days",ofType: "plist") var plistArray = NSArray(contentsOfFile: plistPath!) as NSArray! for obj: AnyObject in plistArray { if var dicInfo = obj as? NSDictionary { let todayTitle: AnyObject? = dicInfo.valueForKey("Title") println(todayTitle) } }
删除可选的一种方法是使用感叹号:
原文链接:https://www.f2er.com/swift/319683.htmlprintln(todayTitle!)
但是,只有当您确定该值在那里时,才应该执行此操作.另一种方法是打开和使用条件,如下所示:
if let theTitle = todayTitle { println(theTitle) }
将此程序粘贴到runswiftlang中进行演示:
let todayTitle : String? = "today" println(todayTitle) println(todayTitle!) if let theTitle = todayTitle { println(theTitle) }