最近新学swift语法,觉得swift语法和OC语法习惯上还是有比较大的区别,我再练习语法的过程中碰到一个问题就是自定义json数据的解析,
在OC里取出数组里字典元素的使用的方式如
NSArray * array = @[
@{
@"name":@"aaa",
@"icon":@"icon1"
},
@{
@"name":@"bbb",
@"icon":@"icon2"
}
];
for (NSDictionary *dic in array) {
NSLog(@"%@",dic[@"name"]);
NSLog(@"%@",dic[@"icon"]);
}
然而到了swift里
lazy var dataArr : Array<Any> = {
let dic:[String: Any] = ["icon":"home_plane_icon","name":"机票"];
let dic1:[String: Any] = ["icon":"home_train_icon","name":"火车"];
let dic2:[String: Any] = ["icon":"home_hotel_icon","name":"酒店"];
let dic3:[String: Any] = ["icon":"home_car_icon","name":"用车"];
var array = Array<Any>.init();
array.append(dic);
array.append(dic1);
array.append(dic2);
array.append(dic3);
return array;
}();
for (index, item) in self.dataArr.enumerated() {
let ss = item as! [String : Any];
let name: String = ss["name"] as! String;
let icon: String = ss["icon"] as! String;
print(name);
print(icon);
}
如有大神希望多指教一下。
网友评论