0
我相信我已經縮小了問題的範圍,以下代碼中的某處我提供了一個不應存在的條件。但由於某些原因,當我運行該應用程序時,它會像self.motorbikes線那樣下降。線程1:EXC_BAD_INSTRUCTION錯誤。試圖解析通過JSON在應用程序中顯示圖像
(請參閱圖)
難道說我的DataSource.swift文件未連接到我的motorbikes.json文件的情況下?
我想完全以編程方式構建,所以不使用故事板對我來說很困難。如果任何人都可以幫助,我會非常感激。
import Foundation
class MotorbikeDataSource {
fileprivate var motorbikes: [Motorbike]?
init() {
self.loadMotorbikes()
}
public func allMotorbikes() -> [Motorbike]? {
return self.motorbikes
}
}
extension MotorbikeDataSource {
fileprivate func loadMotorbikes() {
guard let path = Bundle.main.path(forResource: "motorbikes", ofType: "json") else {
assert(false, "Could not find motorbikes file")
return
}
guard let contents = NSData(contentsOfFile: path) as Data? else {
assert(false, "Could not load motorbikes file: \(path)")
return
}
self.motorbikes = [Motorbike]()
do {
if let jsonDict = try JSONSerialization.jsonObject(with: contents, options: []) as? Dictionary<String, Any> {
if let motorbikes = jsonDict["motorbikes"] as? Array<Dictionary<String, Any>> {
for motorbikeDict in motorbikes {
if let motorbike = Motorbike(dict: motorbikeDict) {
self.motorbikes?.append(motorbike)
}
}
}
}
} catch {
assert(false, "Could not parse motorbikes file: \(path)")
return
}
}
}
什麼是日誌?它是否說了一些關於零? –
你首先要做的事就是初始化它,試着將它作爲[摩托車]() –