NSKeyedUnarchiver.unarchivedObject 字典返回 nil
NSKeyedUnarchiver.unarchivedObject returning nil for dictionary
我正在尝试将我的代码更新为 iOS12。下面是我的代码:
class CurrentGameState: NSObject, NSSecureCoding {
static var supportsSecureCoding: Bool = true
var currentTest1 = Int()
var currentUnitPositionColumn = [String: Int]()
static var currentGameStatus = CurrentGameState(unit: "init", test: 1, column: -1)
var currentTest1 = Int()
var currentUnitPositionColumn = [String: Int]()
init(unit: String, test: Int, column: Int) {
currentTest1 = test
currentUnitPositionColumn[unit] = column
}
func encode(with aCoder: NSCoder) {
aCoder.encode(currentTest1, forKey: test1)
aCoder.encode(currentUnitPositionColumn, forKey: currentColumn)
}
required init(coder aDecoder: NSCoder) {
self.currentTest1 = aDecoder.decodeInteger(forKey: test1)
self.currentUnitPositionColumn = aDecoder.decodeObject(forKey: currentColumn) as! [String : Int]
}
extension PersistData {
func newSaveMatchInfo(saveType: GlobalConstants.saveType) {
let gameDataFile = "GameData" + String(describing: saveType)
if let url = getFileURLInAppDocumentDirectory(fileName: gameDataFile) {
do{
let archive = try NSKeyedArchiver.archivedData(withRootObject: CurrentGameState.currentGameStatus, requiringSecureCoding: true)
try archive.write(to: url)
}catch{
print("!!!!!Error saving Game data == \(error)")
}
}
}
func newLoadMatchInfo(saveType: GlobalConstants.saveType) {
let gameDataFile = "GameData" + String(describing: saveType)
guard let gameData = try? Data(contentsOf: getFileURLInAppDocumentDirectory(fileName: gameDataFile)!),
let gameInfo = try? NSKeyedUnarchiver.unarchivedObject(ofClasses: [CurrentGameState.self], from: gameData as Data ) else {
print("!!!!!Error gameInfo = try? NSKeyedUnarchiver.unarchivedObject failed ")
return
}
CurrentGameState.currentGameStatus = gameInfo as!
}
}
当我 运行 这段代码时,NSKeyedUnarchiver 为 Int currentTest1 工作,但是当它命中字典 currentUnitPositionColumn 时 returns nil。下面显示的 NSKeyedUnarchiver 的旧代码工作正常,但 unarchiveObject(withFile: 在 iOS12.
中已弃用
func loadMatchInfo(saveType: GlobalConstants.saveType) {
let gameDataFile = "GameData" + String(describing: saveType)
if let url = getFileURLInAppDocumentDirectory(fileName: gameDataFile) {
if let gameData = NSKeyedUnarchiver.unarchiveObject(withFile: url.path) as? CurrentGameState {
CurrentGameState.currentGameStatus = gameData
}
}
}
词典中有些内容不适用于我的新代码。任何帮助将不胜感激。
如果您替换:
,您的代码可以正常工作
let gameInfo = try? NSKeyedUnarchiver.unarchivedObject(ofClasses: [CurrentGameState.self], from: gameData as Data ) else {
与:
let gameInfo = try? NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(gameData) else {
我怀疑使用 unarchivedObject(ofClasses:
失败,因为您无法列出 Dictionary<String, Int>
。
我正在尝试将我的代码更新为 iOS12。下面是我的代码:
class CurrentGameState: NSObject, NSSecureCoding {
static var supportsSecureCoding: Bool = true
var currentTest1 = Int()
var currentUnitPositionColumn = [String: Int]()
static var currentGameStatus = CurrentGameState(unit: "init", test: 1, column: -1)
var currentTest1 = Int()
var currentUnitPositionColumn = [String: Int]()
init(unit: String, test: Int, column: Int) {
currentTest1 = test
currentUnitPositionColumn[unit] = column
}
func encode(with aCoder: NSCoder) {
aCoder.encode(currentTest1, forKey: test1)
aCoder.encode(currentUnitPositionColumn, forKey: currentColumn)
}
required init(coder aDecoder: NSCoder) {
self.currentTest1 = aDecoder.decodeInteger(forKey: test1)
self.currentUnitPositionColumn = aDecoder.decodeObject(forKey: currentColumn) as! [String : Int]
}
extension PersistData {
func newSaveMatchInfo(saveType: GlobalConstants.saveType) {
let gameDataFile = "GameData" + String(describing: saveType)
if let url = getFileURLInAppDocumentDirectory(fileName: gameDataFile) {
do{
let archive = try NSKeyedArchiver.archivedData(withRootObject: CurrentGameState.currentGameStatus, requiringSecureCoding: true)
try archive.write(to: url)
}catch{
print("!!!!!Error saving Game data == \(error)")
}
}
}
func newLoadMatchInfo(saveType: GlobalConstants.saveType) {
let gameDataFile = "GameData" + String(describing: saveType)
guard let gameData = try? Data(contentsOf: getFileURLInAppDocumentDirectory(fileName: gameDataFile)!),
let gameInfo = try? NSKeyedUnarchiver.unarchivedObject(ofClasses: [CurrentGameState.self], from: gameData as Data ) else {
print("!!!!!Error gameInfo = try? NSKeyedUnarchiver.unarchivedObject failed ")
return
}
CurrentGameState.currentGameStatus = gameInfo as!
}
}
当我 运行 这段代码时,NSKeyedUnarchiver 为 Int currentTest1 工作,但是当它命中字典 currentUnitPositionColumn 时 returns nil。下面显示的 NSKeyedUnarchiver 的旧代码工作正常,但 unarchiveObject(withFile: 在 iOS12.
中已弃用func loadMatchInfo(saveType: GlobalConstants.saveType) {
let gameDataFile = "GameData" + String(describing: saveType)
if let url = getFileURLInAppDocumentDirectory(fileName: gameDataFile) {
if let gameData = NSKeyedUnarchiver.unarchiveObject(withFile: url.path) as? CurrentGameState {
CurrentGameState.currentGameStatus = gameData
}
}
}
词典中有些内容不适用于我的新代码。任何帮助将不胜感激。
如果您替换:
,您的代码可以正常工作let gameInfo = try? NSKeyedUnarchiver.unarchivedObject(ofClasses: [CurrentGameState.self], from: gameData as Data ) else {
与:
let gameInfo = try? NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(gameData) else {
我怀疑使用 unarchivedObject(ofClasses:
失败,因为您无法列出 Dictionary<String, Int>
。