SpriteKit Swift 2 无法写入 plist
Posted
技术标签:
【中文标题】SpriteKit Swift 2 无法写入 plist【英文标题】:SpriteKit Swift 2 Trouble writing to plist 【发布时间】:2016-01-08 21:37:59 【问题描述】:嗨,有人能指出我正确的方向吗?我正在使用最新的 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】:所以我今天早上很早就醒了,以为我要破解这个我把这行放在保存 plist 函数中
print("plst \(plistPathInDocument)")
这告诉我 .plist 丢失了 所以我添加了
MainArray.writeToFile(plistPathInDocument + ".plist", atomically: true)
所以现在它完全可以工作了整个代码希望这对某人有所帮助,因为我很难在 Swift 2 中找到如何做到这一点
用于保存 plist 名称的变量,以便您可以轻松交换 plist
var NameOfPlist = "Data"
如果 plist 不存在则将其移动到文档并阅读它的功能
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)")
【讨论】:
以上是关于SpriteKit Swift 2 无法写入 plist的主要内容,如果未能解决你的问题,请参考以下文章
无法使用 Xcode 8 和 Swift 3 在 SpriteKit 中以编程方式触发 segue
无法为 swift Spritekit 创建暂停、重启和主页按钮对话框