对 NSArray 在读取 JSON 解析时分割故障swift

标签: json ios Swift
发布时间: 2017/3/6 22:00:03
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

还有一种方法用来填充从 json 的领域数据库︰

func parseJSON(data: NSData) -> NSArray? {
    do {
        let array: NSArray = try NSJSONSerialization.JSONObjectWithData(data, options: .MutableContainers) as! NSArray

        return array
    } catch _ {
        return nil
    }
}
//parameter came from previous method
func updateDatabaseFromParsedJson(parsedJson: NSArray) {
    let realm = try! Realm()
    try! realm.write {
        realm.deleteAll()
    }
    for i in 0..<parsedJson.count {
        let deviceObject = parsedJson[i]

        let name = deviceObject["name"] as! String
        let id = deviceObject["id"] as! Int

        var device = Device()
        device.name = name
        device.id = id
        try! realm.write {
            realm.add(device)
        }
        var deviceMeasuresArray = deviceObject["measures"] as! NSArray
        for i in 0..<deviceMeasuresArray.count {
            var measureObject = deviceMeasuresArray[i]
            var measure = Measure()
            measure.slug = measureObject["name"]
            measure.device = device
            measure.localize()
            try! realm.write {
                realm.add(measure)
            }
            var measureEntriesArray = measureObject["averages"] as! NSArray
            for i in 0..<measureEntriesArray.count {
                var entryObject = measureEntriesArray[i]
                var entry = PeriodAverage()
                entry.measure = measure
                entry.value = entryObject["value"]
                entry.start = NSDate.parse(entryObject["start"])
                entry.end = NSDate.parse(entryObject["end"])
                entry.length = entryObject["length"]
                try! realm.write {
                    realm.add(entry)
                }
            }
        }
    }
}

extension NSDate {
    class func parse(dateString: String) -> NSDate {

        let format = "yyyy-MM-dd'T'HH:mm:ss'Z'"
        let formatter = NSDateFormatter()
        formatter.dateFormat = format
        return formatter.dateFromString(dateString)!
    }
}

JSON 本身http://188.166.51.200/api/v1/actual_data/

编译时出现错误Error:unable to execute command: Segmentation fault: 11

我错在哪里以及如何正确地解析我 json 吗?我觉得问题在行 json 字段公力解析到对象,但我在新的 swift 并不能完全确定的错误。

解决方法 1:

你显然已经遇到一个编译器问题 (这你应该到苹果的报告),虽然很容易解决。 如果您使用的 Swift 你真的应该使用 Swift 集合类型相反 Foundation 集合类型如果可能的话 (即, Array 而不是 NSArray ),允许更多的类型信息。 此外,虽然你的投射的某些值你就得从你的 JSON,你不投射他们所有。 添加此额外的类型信息会使编译器的行为和变通解决这一问题。 我建议以下编辑︰

func updateDatabaseFromParsedJson(parsedJson: Array<AnyObject>) {
    let realm = try! Realm()
    try! realm.write {
        realm.deleteAll()
    }
    for i in 0..<parsedJson.count {
        let deviceObject = parsedJson[i] as! Dictionary<String, AnyObject>

        let name = deviceObject["name"] as! String
        let id = deviceObject["id"] as! Int

        var device = Device()
        device.name = name
        device.id = id
        try! realm.write {
            realm.add(device)
        }
        var deviceMeasuresArray = deviceObject["measures"] as! Array<AnyObject>
        for i in 0..<deviceMeasuresArray.count {
        var measureObject = deviceMeasuresArray[i] as! Dictionary<String, AnyObject>
            var measure = Measure()
            measure.slug = measureObject["name"] as! String // I'm guessing on the type here
            measure.device = device
            measure.localize()
            try! realm.write {
                realm.add(measure)
            }
            var measureEntriesArray = measureObject["averages"] as! Array<AnyObject>
            for i in 0..<measureEntriesArray.count {
                var entryObject = measureEntriesArray[i] as! Dictionary<String, AnyObject>
                var entry = PeriodAverage()
                entry.measure = measure
                entry.value = entryObject["value"] as! String // Guessing on the type here also
                entry.start = NSDate.parse(entryObject["start"] as! String)
                entry.end = NSDate.parse(entryObject["end"] as! String)
                entry.length = entryObject["length"] as! String // Again, guessing on the type here
                try! realm.write {
                    realm.add(entry)
                }
            }
        }
    }
}

无关的编译器问题,您也可以使用为中循环,使您的代码更 Swift 像。 你可以阅读更多关于他们的控制流章的For-In 循环 Swift 编程语言

赞助商