gpt4 book ai didi

JSON NSBundle 错误 - 线程 1 :EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, 子代码 = 0x0)

转载 作者:行者123 更新时间:2023-11-28 09:27:00 24 4
gpt4 key购买 nike

我在解析 JSON 时遇到问题。

这是JSON的结构:

[{"id":33,"name":"5","sort":2},{"id":34,"name":"6","sort":3},{"id":35,"name":"7","sortOrder":4}]

运行后出现 fatal error :

Thread 1:EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)” with code "fatalError("error")"

问题出在 NSBundle 上。我想是因为我添加了代码:

guard let path = NSBundle.mainBundle().pathForResource("sections", ofType: "json"),
let jsonData = NSData(contentsOfFile: path) else {
fatalError("error")
}
do {
if let sections = try NSJSONSerialization.JSONObjectWithData(jsonData,
options: .MutableContainers) as? [String: AnyObject] {
// work with sections
let addr = Address(dict: sections)
print("sections")
}
} catch let error as NSError {
print(error)
}

但是没有“节”。事实上,与 JSON 的链接没有路径——只有对象(id、名称、排序等)。那么我应该如何改变 guard let path = NSBundle 来解决这个问题呢?

最佳答案

为此,您需要检查“DocumentDirectory 和 UserDomainMask”中的文件,需要检查文件是否可用,

   let resourcePath = (bundle as! NSBundle).pathForResource("sections", ofType: "Json")

如果文件可用,以上代码将在您的目录中查找文件。

let resourcePath = self.bundle.pathForResource("sections", ofType: "Json");

P.S 请检查您的文件是否在 copyBundleResources 中。

关于JSON NSBundle 错误 - 线程 1 :EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, 子代码 = 0x0),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36472508/

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