"fatal error: unexpectedly found nil while unwrapping an Optional value" received when using NSFileManager to retrieve multiple values
"fatal error: unexpectedly found nil while unwrapping an Optional value" received when using NSFileManager to retrieve multiple values
当我尝试使用 NSFileManager 检索多个值时收到以下错误:fatal error: unexpectedly found nil while unwrapping an Optional value
这是我的代码:
class func loadGameData() -> (HighScore: Int, HasCompletedTutorial: Bool) {
// getting path to GameData.plist
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) as NSArray
let documentsDirectory = paths[0] as! String
let path = documentsDirectory.stringByAppendingPathComponent("GameData.plist")
let fileManager = NSFileManager.defaultManager()
//check if file exists
if(!fileManager.fileExistsAtPath(path)) {
// If it doesn't, copy it from the default file in the Bundle
if let bundlePath = NSBundle.mainBundle().pathForResource("GameData", ofType: "plist") {
let resultDictionary = NSMutableDictionary(contentsOfFile: bundlePath)
fileManager.copyItemAtPath(bundlePath, toPath: path, error: nil)
}
}
let resultDictionary = NSMutableDictionary(contentsOfFile: path)
var myDict = NSDictionary(contentsOfFile: path)
if let dict = myDict {
//loading values - THIS IS WHERE THE ERROR OCCURS
let HighScore: AnyObject = dict.objectForKey("HighScore")!
let CompletedTutorial: AnyObject = dict.objectForKey("HasCompletedTutorial")!
return (Int(HighScore as! NSNumber), Bool(CompletedTutorial as! NSNumber))
}
return (0, false)
}
我已经亲自测试了这两条线,它们运行良好。但他们似乎并没有一起工作
这里是调用函数的代码
let val = GameData.loadGameData()
println(val.HighScore)
println(val.HasCompletedTutorial)
我已经测试了这个函数调用的多个变体,但没有什么不同
谢谢
你为什么不打开它们?尝试这样的事情
if let dict = myDict {
if let
highScore = dict.objectForKey("HighScore"),
completedTutorial = dict.objectForKey("HasCompletedTutorial")
{
return (Int(highScore as! NSNumber), Bool(completedTutorial as! NSNumber))
}
}
当我尝试使用 NSFileManager 检索多个值时收到以下错误:fatal error: unexpectedly found nil while unwrapping an Optional value
这是我的代码:
class func loadGameData() -> (HighScore: Int, HasCompletedTutorial: Bool) {
// getting path to GameData.plist
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) as NSArray
let documentsDirectory = paths[0] as! String
let path = documentsDirectory.stringByAppendingPathComponent("GameData.plist")
let fileManager = NSFileManager.defaultManager()
//check if file exists
if(!fileManager.fileExistsAtPath(path)) {
// If it doesn't, copy it from the default file in the Bundle
if let bundlePath = NSBundle.mainBundle().pathForResource("GameData", ofType: "plist") {
let resultDictionary = NSMutableDictionary(contentsOfFile: bundlePath)
fileManager.copyItemAtPath(bundlePath, toPath: path, error: nil)
}
}
let resultDictionary = NSMutableDictionary(contentsOfFile: path)
var myDict = NSDictionary(contentsOfFile: path)
if let dict = myDict {
//loading values - THIS IS WHERE THE ERROR OCCURS
let HighScore: AnyObject = dict.objectForKey("HighScore")!
let CompletedTutorial: AnyObject = dict.objectForKey("HasCompletedTutorial")!
return (Int(HighScore as! NSNumber), Bool(CompletedTutorial as! NSNumber))
}
return (0, false)
}
我已经亲自测试了这两条线,它们运行良好。但他们似乎并没有一起工作
这里是调用函数的代码
let val = GameData.loadGameData()
println(val.HighScore)
println(val.HasCompletedTutorial)
我已经测试了这个函数调用的多个变体,但没有什么不同
谢谢
你为什么不打开它们?尝试这样的事情
if let dict = myDict {
if let
highScore = dict.objectForKey("HighScore"),
completedTutorial = dict.objectForKey("HasCompletedTutorial")
{
return (Int(highScore as! NSNumber), Bool(completedTutorial as! NSNumber))
}
}