如何在没有"Optional"的情况下从plist打印字符串? [英] How to print a string from plist without "Optional"?
本文介绍了如何在没有"Optional"的情况下从plist打印字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我从plist中的字典中加载了一个值,但是当我将其打印到控制台时,它会打印:Optional(星期一标题),而不仅仅是星期一标题".
I load a value from a dictionary in a plist but when I print it to the console, it prints: Optional(Monday Title) rather than just "Monday Title".
如何在打印时摆脱值的Optional()?
How can I get rid of the Optional() of my value when printing?
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)
}
}
推荐答案
摆脱Optional
的一种方法是使用感叹号:
One way to get rid of the Optional
is to use an exclamation point:
println(todayTitle!)
但是,只有在确定存在该值的情况下,才应该这样做.另一种方法是解开包装并使用条件式,如下所示:
However, you should do it only if you are certain that the value is there. Another way is to unwrap and use a conditional, like this:
if let theTitle = todayTitle {
println(theTitle)
}
将此程序粘贴到 runswiftlang 中进行演示:
Paste this program into runswiftlang for a demo:
let todayTitle : String? = "today"
println(todayTitle)
println(todayTitle!)
if let theTitle = todayTitle {
println(theTitle)
}
这篇关于如何在没有"Optional"的情况下从plist打印字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文