gpt4 book ai didi

ios - 快速读取 JSON 解析到 NSArray 时出现段错误

转载 作者:行者123 更新时间:2023-11-29 00:58:29 25 4
gpt4 key购买 nike

有一种方法用于从 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/

编译时出现错误错误:无法执行命令:段错误:11

我错在哪里以及如何正确解析我的 json?我认为问题出在 json 字段强制解析为对象的行中,但我是 swift 新手,无法准确确定错误。

最佳答案

您显然遇到了编译器问题(您应该 report to Apple ),尽管它很容易解决。如果您正在使用 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)
}
}
}
}
}

与编译器问题无关,您还可以使用 for-in 循环使您的代码更像 Swift。您可以在 For-In Loops 中阅读有关它们的更多信息。节Control Flow The Swift Programming Language的章节.

关于ios - 快速读取 JSON 解析到 NSArray 时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37328889/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com