gpt4 book ai didi

swift - 保存/加载文件: "warning: dynamic accessors failed to find @property implementation for ' uniqueId'"

转载 作者:行者123 更新时间:2023-11-30 12:55:45 25 4
gpt4 key购买 nike

我正在尝试将对象保存/加载到文件,为此我只是将对象(self.game)转换为包含字典的数组,然后使用 write(toFile:atomically :)。但 saveGame 方法不执行任何操作:我可以选择一个文件,按“确定”,保存面板将被关闭,但该文件并未真正保存。当我使用 loadGame 方法时,我发现出现了一个奇怪的错误:

warning: dynamic accessors failed to find @property implementation for 'uniqueId' for entity ABCDInfo while resolving selector 'uniqueId' on class 'ABCDInfo'. Did you remember to declare it @dynamic or @synthesized in the @implementation ?

@IBAction func saveGame(_ sender: NSButton) {
let savePanel = NSSavePanel()
savePanel.allowedFileTypes = ["com.apple.property-list"]
if savePanel.runModal() == NSFileHandlingPanelOKButton {
let file = savePanel.url!.absoluteString
self.game.write(toFile: file)
}
}

@IBAction func loadGame(_ sender: NSButton) {
let openPanel = NSOpenPanel()
openPanel.allowedFileTypes = ["com.apple.property-list"]
if openPanel.runModal() == NSFileHandlingPanelOKButton {
let file = openPanel.url!.absoluteString
self.game = Game(withFile: file)
self.selectedSlot = nil
self.enemySlot = nil
self.restartButton.isEnabled = false
self.updateTurnLabel()
self.chessboardView.reloadData()
}
}

self.game.write(toFile:) 方法只是将对象转换为 NSArray 并将其写入作为参数传递的文件中。

enter image description here enter image description here

最佳答案

当你用字典保存数组时,你可以简单地使用 Objective-C 中的writeToFile。但对于 Swift 来说情况并非如此,除非键绝对是字符串对象。您必须首先使用 NSKeyedArchiver 对其进行存档。然后您可以将其写入文件。如果您不存档它,则最终可能会没有任何文件,无论是否有警告。下面是一个例子。

let title = recordArray[r1].valueForKey("texttitle") as! String
let text = recordArray[r1].valueForKey("texttext") as! String
let urls = recordArray[r1].valueForKey("urls") as! String
var codeArray = [NSDictionary]()
let dict = ["Title":title,"URLs":urls,"Code":text] as NSDictionary // or Dictionary
codeArray.append(dict)
let data = NSKeyedArchiver.archivedDataWithRootObject(codeArray)
data.writeToFile(path, atomically: true)

打开文件时,使用 NSKeyedUnarchiver 取消存档数据。

关于swift - 保存/加载文件: "warning: dynamic accessors failed to find @property implementation for ' uniqueId'",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40425585/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com