SpriteKit Swift 2无法写入plist

时间:2016-01-08 21:37:59

标签: plist

嗨,有人可以指出我正确的方向我正在使用最新的Xcode和swift我已经成功地复制了plist并阅读它但它不会写吗?任何帮助非常感谢

var NameOfPlist = "Data"

func CopyPlistAndReadContentsToAnArray(NameOfPlist:String)
{

let url = NSURL(string: NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, .UserDomainMask, true)[0])
let  plistPathInDocument = (url?.URLByAppendingPathComponent(NameOfPlist + ".plist").absoluteString)!

if !NSFileManager.defaultManager().fileExistsAtPath(plistPathInDocument)
{
    let plistPathInBundle = NSBundle.mainBundle().pathForResource(NameOfPlist, ofType: "plist")!
    do
    {
        try NSFileManager.defaultManager().copyItemAtPath(plistPathInBundle, toPath: plistPathInDocument)
        let levelPlist =  plistPathInDocument
        MainArray = NSMutableArray(contentsOfFile: levelPlist)!
        print("plist copied")
    }
    catch
    {
        print("error copying plist!")
    }
}
else
{
    let levelPlist =  plistPathInDocument
    MainArray = NSMutableArray(contentsOfFile: levelPlist)!
    print("PlistInDocOk")
    // print("plst  \(plistPathInDocument)") // = Data/Application/7D6891BB-2EE5-4D1A-8EB5-73B8100F0827/Documents/Data.plist
    // print("This is the MainArray \(MainArray)")
}


}


func SaveToPlist(NameOfPlist:String)
{
let url = NSURL(string: NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, .UserDomainMask, true)[0])
let  plistPathInDocument = (url?.URLByAppendingPathComponent(NameOfPlist).absoluteString)!
MainArray.writeToFile(plistPathInDocument, atomically: true)
print("Item Added")
}

1 个答案:

答案 0 :(得分:0)

所以我今天早上醒来并且认为我要破解这个我把这一行放在保存plist函数中

print("plst \(plistPathInDocument)")

告诉我.plist丢失了 所以我添加了

MainArray.writeToFile(plistPathInDocument + ".plist", atomically: true)

所以现在它完全正常工作整个代码希望这有助于某人,因为我很难在Swift 2中找到如何做到这一点

变量用于保存plist名称,以便您可以轻松交换plist

  var NameOfPlist = "Data"

将plist移动​​到docs的功能,如果它不在那里并阅读它

func CopyPlistAndReadContentsToAnArray(NameOfPlist:String)
{

let url = NSURL(string: NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, .UserDomainMask, true)[0])
let  plistPathInDocument = (url?.URLByAppendingPathComponent(NameOfPlist + ".plist").absoluteString)!

if !NSFileManager.defaultManager().fileExistsAtPath(plistPathInDocument)
{
    let plistPathInBundle = NSBundle.mainBundle().pathForResource(NameOfPlist, ofType: "plist")!
    do
    {
        try NSFileManager.defaultManager().copyItemAtPath(plistPathInBundle, toPath: plistPathInDocument)
        let levelPlist =  plistPathInDocument
        MainArray = NSMutableArray(contentsOfFile: levelPlist)!
        print("plist copied")
    }
    catch
    {
        print("error copying plist!")
    }
}
else
{
    let levelPlist =  plistPathInDocument
    MainArray = NSMutableArray(contentsOfFile: levelPlist)!
    print("Plist In Doc Ok")
    print("plst  \(plistPathInDocument)") // = Data/Application/7D6891BB-2EE5-4D1A-8EB5-73B8100F0827/Documents/Data.plist
    // print("This is the MainArray \(MainArray)")
   }


}

保存到plist的功能

func SaveToPlist(NameOfPlist:String)
{

let url = NSURL(string: NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, .UserDomainMask, true)[0])
let  plistPathInDocument = (url?.URLByAppendingPathComponent(NameOfPlist).absoluteString)!
MainArray.writeToFile(plistPathInDocument + ".plist", atomically: true)
print("Item Added")
 print("plst  \(plistPathInDocument)")

}