我正在使用紧凑映射从json数组[[String:Any]]中获取模型(NSObject)。
我进行了一些测试,似乎需要一些时间才能从json数组中获取对象。 json数组中的300个对象大约需要1秒。
问题是我正在处理大量应用程序数据,并且可以在json数组中容纳数千个对象。
class ModelObject: NSObject {
var data: Data?
var json: [String: Any]?
init(withJSON json: [String: Any]?) {
data = json?.toData
self.json = json
}
init(withId id: String? = nil,withType type: String? = nil,withStatus status: Int? = nil,withName name: String? = nil) {
super.init()
updateContent(withId: id,withType: type,withStatus: status,withName: name)
}
func updateContent(withJSON json: [String: Any]? = nil,withId id: String? = nil,withName name: String? = nil) {
var json = self.json ?? [String: Any]()
if let id = id {
json["_id"] = id
}
if let type = type {
json["type"] = type
}
if let status = status {
json["status"] = status
}
if let name = name {
json["name"] = name
}
}
}
extension Dictionary {
var toData: Data? {
return try? JSONSerialization.data(withJSONObject: self,options: [])
}
}
还有其他减少时间或立即获取时间的可能性吗? 这是我的代码:
return fileJSONArray.compactMap { ModelObject(withJSON: $0) }