提问人:Guitarman4 提问时间:8/18/2022 最后编辑:Guitarman4 更新时间:8/21/2022 访问量:75
自 iOS 12.0 起,如何使用 UserDefaults 存档和取消存档 SKPhysicsBody 数组
How to archive and unarchive an SKPhysicsBody array using UserDefaults since iOS 12.0
问:
我可以使用 UserDefaults 成功存档和取消存档 SKPhysicsBody 数组,如下所示,但取消存档过程已被弃用。这是警告消息... 'unarchiveObject(with:)' 在 iOS 12.0 中已弃用:请改用 +unarchivedObjectOfClass:fromData:error: 。我一直在兜兜转转,但仍然没有工作。我将如何遵守这一点?我的代码如下。
要存档...
do { try UserDefaults.standard.set(NSKeyedArchiver.archivedData(withRootObject:Sat1shapes,requiringSecureCoding:true), forKey: "Sat1shapes")
}
catch {
print(error)}
要取消存档...
var Sat1shapes: [SKPhysicsBody] = []
if let dataObject = UserDefaults.standard.value(forKey: "Sat1shapes") as? NSData {
Sat1shapes = NSKeyedUnarchiver.unarchiveObject(with: dataObject as Data) as! [SKPhysicsBody]
}
下面是避免警告的更新代码,但它不像已弃用的版本那样存储 [SKPhysicsBody] 数组。
var Sat1shapes: [SKPhysicsBody] = []
let dataObject = UserDefaults.standard.value(forKey: "Sat1shapes") as? NSData
do {
Sat1shapes = try NSKeyedUnarchiver.unarchivedObject(ofClasses: [NSArray.self, SKPhysicsBody.self], from: dataObject! as Data) as! [SKPhysicsBody]
} catch {
print(error)
}
答:
0赞
VaporwareWolf
8/18/2022
#1
像这样的东西。查看流程中的每个步骤,找出您遇到问题的地方。
func writeBody(_ body: SKPhysicsBody) {
do {
// Convert from SKPhysicsBody to Data
let data = try NSKeyedArchiver.archivedData(withRootObject: body, requiringSecureCoding: false)
// Write Data instance into UserDefaults
UserDefaults.standard.set(data, forKey: "body")
print("Wrote body: \(String(reflecting: body))")
} catch {
print("Failed to write body: \(error.localizedDescription)")
}
}
func readBody() -> SKPhysicsBody? {
// Read body from UserDefaults (as Data).
guard let dataObject = UserDefaults.standard.object(forKey: "body") else {
print(#"No data was stored in UserDefaults under "body" key"#)
return nil
}
guard let data = dataObject as? Data else {
print(#"Object stored in UserDefaults "body" key is not Data type."#)
return nil
}
do {
return try NSKeyedUnarchiver.unarchivedObject(ofClass: SKPhysicsBody.self, from: data)
} catch {
print("Failed to unarchive SKPhysicsBody from Data: \(error.localizedDescription)")
return nil
}
}
更新:添加数组变体
func writeBodies(_ bodies: [SKPhysicsBody]) {
do {
// Convert from SKPhysicsBody to Data
let data = try NSKeyedArchiver.archivedData(withRootObject: bodies, requiringSecureCoding: false)
// Write Data instance into UserDefaults
UserDefaults.standard.set(data, forKey: "bodies")
print("Wrote bodies: \(String(reflecting: bodies))")
} catch {
print("Failed to write bodies: \(error.localizedDescription)")
}
}
func readBodies() -> [SKPhysicsBody]? {
// Read body from UserDefaults (as Data).
guard let dataObject = UserDefaults.standard.object(forKey: "bodies") else {
print(#"No data was stored in UserDefaults under "bodies" key"#)
return nil
}
guard let data = dataObject as? Data else {
print(#"Object stored in UserDefaults "bodies" key is not Data type."#)
return nil
}
do {
return try NSKeyedUnarchiver.unarchivedArrayOfObjects(ofClass: SKPhysicsBody.self, from: data)
} catch {
print("Failed to unarchive SKPhysicsBody from Data: \(error.localizedDescription)")
return nil
}
}
评论
0赞
Guitarman4
8/18/2022
谢谢。这似乎适用于单个 SKPhysicsBody,但是如果我尝试保存 [SKPhysicsBody] 数组,它会抛出两个错误。静态方法“unarchivedObject(ofClass:from:)”要求“[SKPhysicsBody]”符合“NSCoding”,静态方法“unarchivedObject(ofClass:from:)”要求“[SKPhysicsBody]”继承自“NSObject”。@VaporwareWolf
0赞
Guitarman4
8/18/2022
创建类 NSArray.self 会停止错误并运行,但似乎无法正确存储。这是更新后的行...NSKeyedUnarchiver.unarchivedObject(ofClass: NSArray.self, from: dataObject as!数据)作为![SKPhysicsBody]...有什么建议吗?@VaporwareWolf
0赞
VaporwareWolf
8/21/2022
嗨,@Guitarman4,我已经修改了上面的帖子。如果这对您有帮助,请接受答案。
0赞
Guitarman4
8/22/2022
@ VaporwareWolf ,感谢您更新代码。恐怕它仍然无法处理错误消息。“无法从数据中取消存档 SKPhysicsBody:无法读取数据,因为它的格式不正确。”有趣的是,我发现unarchiveTopLevelObjectWithData运行良好,并且不会像unarchiveObject(with:)那样抛出已弃用的减弱。这是否意味着它仍然可以使用?
评论
unarchiveTopLevelObjectWithData