【发布时间】:2019-12-20 10:06:35
【问题描述】:
我有来自URL 的parsed JSON 数据和array 上的subscriber 触发器,因为array 被填充。但是我从onNext 得到的数据看起来像这样:MyProject.People。如何获得实际值?这是我的代码:
guard let myURL = URL(string: "https://api.myjson.com/bins/e5gjk") else { return }
var myArray: Variable<[People]> = Variable([])
myArray.asObservable().subscribe(onNext: { arrayData in
print("TRIGGERED", arrayData)
}).disposed(by: bag)
Alamofire.request(myURL, method: .get)
.validate()
.responseJSON{ response in
guard response.result.isSuccess else {
print("Error")
return
}
let json = JSON(response.result.value)
for i in 0...json["employees"].count {
let people = People()
people.name = json["employees"][i]["firstName"].stringValue
people.job = json["employees"][i]["job"].stringValue
myArray.value.append(people)
}
for i in myArray.value {
print(i.name)
print(i.job)
}
}
所以,arrayData 返回 MyProject.People 但应该给出字符串。我试过arrayData.name 和arrayData.value.name 但它没有显示任何东西。 People 看起来像这样:
class People {
var name = ""
var job = ""
}
【问题讨论】:
-
你尝试调试了吗?看看你的
json结果?而且我相信这条线会崩溃,因为索引会超出范围for i in 0...json["employees"].count -
如果我在填充了数组的 for 循环之后打印,我可以看到
myArray中的所有数据,以便该部分工作。也不要撞车。但我认为你是对的,因为循环运行的时间比实际数据多一次,所以我会调查一下,谢谢!
标签: json swift alamofire rx-swift swifty-json