我希望将以下json放到我的数组中,以便在UITableview
{
MyPets = (
{
breed = "";
breedvaccinationrecord = "";
"city_registration" = "";
"date_of_birth" = "";
"emergency_contacts" = "";
gender = m;
"pet_id" = 475;
"pet_name" = "IOS PET";
petinfo = "http://name/pet_images/";
"prop_name" = "";
"qr_tag" = 90909090;
species = Canine;
"vaccination_records" = "";
vaccinationrecord = "http://Name/vaccination_records/";
"vet_info" = "";
}
);
}
我正在使用下面的代码将值输入数组
if let dict = response.result.value {
print(response)
let petListArray = dict as! NSDictionary
self.petListArray = petListArray["MyPets"] as! [NSMutableArray]}
在cellForRowAtIndexPath中的我使用这一行在TableCell中的UILabel中显示名称
cell?.petName.text = self.petListArray[indexPath.row].valueForKey("pet_name") as? String
但它像
一样崩溃fatal error: NSArray element failed to match the Swift Array Element type
我是swift 2的新手,请提前帮助我
答案 0 :(得分:0)
首先声明petListArray
为Swift Array
,不使用Swift 中的NSMutable...
集合类型。< / p>
顺便说一句,命名...ListArray
是多余的,我建议petList
或petArray
或简称pets
:
var pets = [[String:Any]]()
根对象是一个字典,键MyPets
的值是一个数组,所以强制转换
if let result = response.result.value as? [String:Any],
let myPets = result["MyPets"] as? [[String:Any]] {
self.pets = myPets
}
在cellForRow
写
let pet = self.pets[indexPath.row]
cell?.petName.text = pet["pet_name"] as? String
高度重新考虑使用自定义类或结构作为模型。这避免了很多类型的铸造。