如果这听起来很愚蠢,我很抱歉,但是 JSONDecoder 从哪里获取数据?我不断得到“网格”和“红色”,所以我认为它只是解码我设置的这个虚拟对象,而不是上次程序运行时的 JSONEncoder().encode(myStarageObject)。这是我通过查看在线教程和示例管理的代码:
private func loadMyObject() -> MyObject? {
let jsonString = """
{
"type":"Grid",
"color":"Red",
"lineThickness":2
}
"""
if let jsonData = jsonString.data(using: .utf8) {
do {
let loadedObject = try JSONDecoder().decode(myStorageStruct.self,from: jsonData)
print("I loaded type \(loadedObject.type) of color: \(loadedObject.color)")
return makeMyObjectFromStorageData(loadedObject)
} else {
return nil
}
} catch let error { print(error) }
} else {
return nil
}
return nil
}