以下是我从网址
获得的 JSON 回复 {
"message" : {
"8" : {
"post_id" : 141,
"post_img" : [
{
"guid" : "http:\/\/xxx\/wp-content\/uploads\/2016\/10\/IMG_1477452361.jpg"
}
]
},
"2" : {
"post_id" : 129,
"post_img" : [
{
"guid" : "http:\/\/xxx\/wp-content\/uploads\/2016\/10\/IMG_1477280037.jpg"
}
]
},
"9" : {
"post_id" : 143,
"post_img" : [
{
"guid" : "http:\/\/xxx\/wp-content\/uploads\/2016\/10\/IMG_1477453054.jpg"
}
]
},
"3" : {
"post_id" : 131,
"post_img" : [
{
"guid" : "http:\/\/xxx\/wp-content\/uploads\/2016\/10\/IMG_1477282075.jpg"
}
]
},
"user_posts" : "10",
"4" : {
"post_id" : 133,
"post_img" : [
{
"guid" : "http:\/\/xxx\/wp-content\/uploads\/2016\/10\/IMG_1477393524.jpg"
}
]
},
"user_img" : "http:\/\/www.thewoodjoynt.com\/Content\/Images\/Products\/NoImageAvailable.jpg",
"user_id" : null,
"5" : {
"post_id" : 135,
"post_img" : [
{
"guid" : "http:\/\/xxx\/wp-content\/uploads\/2016\/10\/IMG_1477393867.jpg"
}
]
},
"user_name" : null,
"6" : {
"post_id" : 137,
"post_img" : [
{
"guid" : "http:\/\/xxx\/wp-content\/uploads\/2016\/10\/IMG_1477393932.jpg"
}
]
},
"7" : {
"post_id" : 139,
"post_img" : [
{
"guid" : "http:\/\/xxx\/wp-content\/uploads\/2016\/10\/IMG_1477395902.jpg"
}
]
},
"following" : null,
"followers" : null,
"0" : {
"post_id" : 110,
"post_img" : [
{
"guid" : "http:\/\/xxx\/wp-content\/uploads\/2016\/10\/IMG_1475492476.jpg"
}
]
},
"1" : {
"post_id" : 112,
"post_img" : [
{
"guid" : "http:\/\/xxx\/wp-content\/uploads\/2016\/10\/IMG_1475494067.jpg"
}
]
},
"user_type" : false
}
}
keys are &&&&&&&& ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
fatal error: unexpectedly found nil while unwrapping an Optional value
我正在使用 Alamofire 4& SwiftyJSON 用于处理 JSON 数据,这是我的函数,用于获取数据并将其分配给变量:
func getJSON(){
let getEndPoint: String = "http://xxx/api/get_user_profile_info/"
Alamofire.request(getEndPoint)
.responseJSON { response in
guard response.result.error == nil else {
// got an error in getting the data, need to handle it
print("error calling GET")
print(response.result.error!)
return
}
if let value = response.result.value {
let json = JSON(value)
print(json)
if let dic = json["message"].dictionary {
//for Profile Image
if let userUrl = dic["user_img"]?.stringValue {
self.user_image = URL(string: userUrl)
}
//for collectionView Cell Image
//For getting only number keys with ascending order
let keys = (Array(dic.keys) as [String]).filter { (Int($0) != nil) }.sorted {
(s1, s2) -> Bool in return s1.localizedStandardCompare(s2) == .orderedAscending
}
print("keys are &&&&&&&&",keys)
//Loop through the keys Array and append all `post_image`.
for key in keys {
let post_imgAA = dic[key]?.array
for itemsIMG in post_imgAA! {
self.post_image = itemsIMG["guid"].URL
}
}
print("user_image are***********************************************")
print(self.user_image)
print("post_image are***********************************************")
print(self.post_image)
DispatchQueue.main.async {
self.afData = 1
self.collectionView.reloadData()
}
}
}
}
这里我在循环
中找到了nil值for key in keys {
let post_imgAA = dic[key]?.array
for itemsIMG in post_imgAA! {
self.post_image = itemsIMG["guid"].URL
}
}
处理JSON数据并将其转换为基础对象对我来说有点混乱,因为我在这方面很新。如果任何善良的人可以提供帮助,那将非常有帮助。先感谢您。
答案 0 :(得分:1)
您收到nil
因为key
包含dictionary
而不是Array
,所以改变如此。
for key in keys {
if let innerDic = dic[key].dictionaryValue,
let post_imgAA = innerDic["post_img"].array {
for itemsIMG in post_imgAA! {
self.post_image = itemsIMG["guid"].URL
}
}
}