我的應用從這個API需要一些數據時,Xcode中給出了一個致命的錯誤:https://api.jqestate.ru/v1/properties/countryAPI不包含某些對象的任何值和運行應用程序
GitHub的鏈接到我的項目:https://github.com/armansharvel/JQ-Estate.git(下載分支「提神」 )
沒有編譯器錯誤,但是當我在模擬器中運行我的應用程序Xcode在控制檯「致命錯誤:索引超出範圍」中打印時。
在ObjectModel.swift中,我創建了一個包含某些數據類型的對象的類。其中之一是變量mainPic(我想從API獲取的TableVeiw圖片的URL)。但問題不在於API中的每個對象都包含圖片的URL值。
所以Xcode中(當我嘗試運行應用程序)標誌着初始化mainPic可變碼塊的第二線和所述錯誤是:「螺紋7:EXC_BAD_INSTRUCTION(代碼= EXC_1386_INVOP,子碼=爲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>
謝謝!
非常感謝您!我甚至不知道有第一個操作員。 – Arman