【发布时间】:2017-05-29 03:30:42
【问题描述】:
我有一个包含许多 json 文件的文件夹。例如:
a.json
b.json
c.json
c.json2
c.json3
每个文件都包含我需要插入领域数据库的数据。 a=一种对象,b=另一种对象,c.json1、c.json2、c.json3都是同一种对象,但由于内容多,分为3个文件。
我没有为每种类型的对象分别创建一个 for 循环,而是尝试创建一个字典,我可以为我的第二个 for 循环迭代。
var filesToProcess : [String: Object] =
["a.json" : A(), "b.json" : B(), "c.json" : C()]
for (str, obj) in filesToProcess {
let numFiles = FileCounter().getNumFilesStartingWith(filename : str, url : unzippedDestinationUrl)
for i in 0...numFiles {
var append : String = ""
i == 0 ? (append = "") : (append = String(i))
if let jsonData = try? Data(contentsOf: unzippedDestinationUrl.appendingPathComponent(str+append)){
if let array = (try? JSONSerialization.jsonObject(with: jsonData, options: [])) as? [[String: Any]] {
for item in array{
let itemJsonStr = item["data"] as! String
let item = obj(jsonStr : itemJsonStr)
DispatchQueue(label: "background").async {
let realm = try! Realm()
try! realm.write {
realm.add(item)
}
}
}
}
}
}
}
其中 A、B 和 C 是这样的对象:
import Foundation
import RealmSwift
import Realm
open class A : Object {
open dynamic var _id : String = ""
open dynamic var prop1 : Int = 0
open dynamic var prop2 : String = ""
open override class func primaryKey() -> String? {
return "_id"
}
required public init() {
super.init()
}
public init(jsonStr: String)
{
if let dataDict = try? JSONSerializer.toDictionary(jsonStr){
self._id = dataDict ["id"] as! String
self.prop1 = dataDict ["prop1"] as! Int
self.prop2 = dataDict ["prop2"] as! String
}
super.init()
}
required public init(realm: RLMRealm, schema: RLMObjectSchema) {
super.init(realm: realm, schema: schema)
}
required public init(value: Any, schema: RLMSchema) {
fatalError("init(value:schema:) has not been implemented")
}
}
但是在我的 for 循环中:
let item = obj(jsonStr : itemJsonStr)
我收到错误消息:
Cannot call value of Non-function type 'Object'
这有什么问题吗?我尝试做的事情是否可行,或者我应该坚持我已经做过的事情,即为每种类型的对象创建带有重复代码的单独循环?注意 A、B 和 C 具有不同的属性,但都使用 json 类型的输入字符串初始化
【问题讨论】:
标签: swift string loops object dictionary