ios - API 不包含某些对象的任何值,Xcode 在运行该应用程序时会出现 fatal error

标签 ios json swift api

我的应用从这个 API 获取一些数据:https://api.jqestate.ru/v1/properties/country

我项目的 GitHub 链接:https://github.com/armansharvel/JQ-Estate.git (下载分支“清爽”)

没有编译器错误,但是当我在模拟器中运行我的应用程序时,Xcode 在控制台中打印“ fatal error :索引超出范围”。

在 ObjectModel.swift 中,我创建了一个具有某些数据类型的对象类。其中之一是变量 mainPic(我也想从 API 获取的 TableVeiw 图片的 URL)。但问题不是 API 中的每个对象都包含图片 URL 的值。

所以 Xcode(当我尝试运行应用程序时)标记了初始化 mainPic 变量的代码块的第二行,错误是:“Thread 7: EXC_BAD_INSTRUCTION (code=EXC_1386_INVOP, subcode=0x0)”

这是代码中的整个类:

import Foundation

class Houses {

// Data Encapsulation

private var _mainPic: String
private var _localityName: String
private var _routeName: String
private var _mkadDistance: String
private var _rentOffer: String
private var _saleOffer: String

// Make a getted

var mainPic: String {
    return _mainPic
}

var localityName: String {
    return _localityName
}

var routeName: String {
    return _routeName
}

var mkadDistance: String {
    return _mkadDistance
}

var rentOffer: String {
    return _rentOffer
}

var saleOffer: String {
    return _saleOffer
}

// Initialization

init(data: JSONDictionary) {

    // Main Picture

    if let images = data["images"] as? JSONArray,
        pic0 = images[0] as? JSONDictionary, // THIS LINE IS WITH ERROR
        mainPic = pic0["url"] as? String {
        self._mainPic = mainPic
    } else {
        _mainPic = ""
    }

    // Locality Name

    if let location = data["location"] as? JSONDictionary,
        localityName = location["localityName"] as? String {
        self._localityName = localityName
    } else {
        _localityName = ""
    }

    // Route Name

    if let location = data["location"] as? JSONDictionary,
        routeName = location["routeName"] as? String {
        self._routeName = routeName
    } else {
        _routeName = ""
    }

    // MKAD Distance

    if let location = data["location"] as? JSONDictionary,
        mkadDistance = location["mkadDistance"] as? String {
        self._mkadDistance = mkadDistance
    } else {
        _mkadDistance = ""
    }

    // Rent Offer

    if let rentDict = data["rentOffer"] as? JSONDictionary,
        rentOffer = rentDict["price"] as? String {
        self._rentOffer = rentOffer
    } else {
        _rentOffer = ""
    }

    // Sale Offer

    if let saleDict = data["saleOffer"] as? JSONDictionary,
        saleOffer = saleDict["price"] as? String {
        self._saleOffer = saleOffer
    } else {
        _saleOffer = ""
    }
}
}

以防万一,JSONDictionary 和 JSONArray 只是类型别名:

typealias JSONDictionary = [String : AnyObject]

typealias JSONArray = Array<AnyObject>

提前致谢!

最佳答案

如果 images 数组为空,

images[0] 将崩溃并显示“ fatal error :索引超出范围”。

由于您使用的是可选绑定(bind),因此请先使用 而不是 [0]:

if let images = data["images"] as? JSONArray,
    pic0 = images.first as? JSONDictionary,
    mainPic = pic0["url"] as? String {
    self._mainPic = mainPic
} else {
    _mainPic = ""
}

关于ios - API 不包含某些对象的任何值,Xcode 在运行该应用程序时会出现 fatal error ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37422230/

相关文章:

Swift - '()' 是什么类型?

ios - 如何向 Storyboard快速创建的 UIBarButtonItem 添加 Action ?

ios - UITableView 在快速滚动期间卡住

json - AngularJs:基于模型添加类

python - 在 Python 中列出 JSON 字段

javascript - 将 json 响应转换为表

swift - 如何使用 `JSON.Element` 将 `JSON` 转换为 "SwiftyJSON"?

android - 本地和远程数据的身份验证的体系结构/实现

ios - 自定义 header "fatal error: unexpectedly found nil while unwrapping an Optional value"

ios - 如何在 iOS swift 中将 .opus 文件转换为 .mp3/.m4a/.aac?