iOS 开发后台返回数据分类
2020-08-19 本文已影响0人
我是卖报的小行家
前情提要:
后台返回了一堆数据,格式如下
后台返回的数据
要求:根据area_id进行分类,1-6分别代表不同洲,每个洲最多只取前三个国家,如果超出则用...代替。
效果图
完成后效果图
思路,得到返回数据后,通过area_id遍历数组,得到每个洲,然后在每一中洲里面进行判断字符串拼接。
上代码
1.懒加载模拟数据
-(NSArray *)datasArray
{
if (!_datasArray) {
_datasArray = @[@{@"apn":@"uinternet",@"area_id":@"1",@"country_name":@"亚美尼亚",@"network_type":@"4G"},
@{@"apn":@"3gnet",@"area_id":@"2",@"country_name":@"奥地利",@"network_type":@"Optus"},
@{@"apn":@"3gnet",@"area_id":@"1",@"country_name":@"中国",@"network_type":@"Optus"},
@{@"apn":@"3gnet",@"area_id":@"4",@"country_name":@"澳大利亚",@"network_type":@"Optus"},
@{@"apn":@"3gnet",@"area_id":@"5",@"country_name":@"非洲7",@"network_type":@"Optus"},
@{@"apn":@"3gnet",@"area_id":@"6",@"country_name":@"美国",@"network_type":@"Optus"},
@{@"apn":@"3gnet",@"area_id":@"5",@"country_name":@"非洲6",@"network_type":@"Optus"},
@{@"apn":@"3gnet",@"area_id":@"3",@"country_name":@"乌拉圭",@"network_type":@"Optus"},
@{@"apn":@"3gnet",@"area_id":@"4",@"country_name":@"纽西兰",@"network_type":@"Optus"},
@{@"apn":@"3gnet",@"area_id":@"5",@"country_name":@"非洲5",@"network_type":@"Optus"},
@{@"apn":@"3gnet",@"area_id":@"1",@"country_name":@"日本",@"network_type":@"Optus"},
@{@"apn":@"3gnet",@"area_id":@"2",@"country_name":@"奥地利",@"network_type":@"Optus"},
@{@"apn":@"3gnet",@"area_id":@"5",@"country_name":@"非洲4",@"network_type":@"Optus"},
@{@"apn":@"3gnet",@"area_id":@"6",@"country_name":@"加拿大",@"network_type":@"Optus"},
@{@"apn":@"3gnet",@"area_id":@"3",@"country_name":@"巴拉圭",@"network_type":@"Optus"},
@{@"apn":@"3gnet",@"area_id":@"2",@"country_name":@"奥地利",@"network_type":@"Optus"},
@{@"apn":@"3gnet",@"area_id":@"1",@"country_name":@"韩国",@"network_type":@"Optus"},
@{@"apn":@"3gnet",@"area_id":@"4",@"country_name":@"海盗",@"network_type":@"Optus"},
@{@"apn":@"3gnet",@"area_id":@"5",@"country_name":@"非洲3",@"network_type":@"Optus"},
@{@"apn":@"3gnet",@"area_id":@"6",@"country_name":@"北美1",@"network_type":@"Optus"},
@{@"apn":@"3gnet",@"area_id":@"3",@"country_name":@"阿根廷",@"network_type":@"Optus"},
@{@"apn":@"3gnet",@"area_id":@"2",@"country_name":@"奥地利",@"network_type":@"Optus"},
@{@"apn":@"3gnet",@"area_id":@"1",@"country_name":@"朝鲜",@"network_type":@"Optus"},
@{@"apn":@"3gnet",@"area_id":@"4",@"country_name":@"不知名笑道",@"network_type":@"Optus"},
@{@"apn":@"3gnet",@"area_id":@"5",@"country_name":@"非洲2",@"network_type":@"Optus"},
@{@"apn":@"3gnet",@"area_id":@"6",@"country_name":@"北美2",@"network_type":@"Optus"},
@{@"apn":@"3gnet",@"area_id":@"5",@"country_name":@"非洲1",@"network_type":@"Optus"},
@{@"apn":@"3gnet",@"area_id":@"2",@"country_name":@"奥地利",@"network_type":@"Optus"},
@{@"apn":@"3gnet",@"area_id":@"4",@"country_name":@"大洋洲1",@"network_type":@"Optus"},
@{@"apn":@"3gnet",@"area_id":@"3",@"country_name":@"巴西",@"network_type":@"Optus"}
];
}
return _datasArray;
}
2.处理数据
//定义好每个洲字符串
NSString *countryName= @"";
NSString *asiaStr = @"";
NSString *europStr = @"";
NSString *africaStr = @"";
NSString *oceanStr = @"";
NSString *northAmericaStr = @"";
NSString *southAmericaStr = @"";
//定义好每个洲所拥有的的国家的个数
NSInteger asiaCount = 0;
NSInteger europCount = 0;
NSInteger africaCount = 0;
NSInteger oceanCount = 0;
NSInteger northAmericaCount = 0;
NSInteger southAmericaCount = 0;
3.遍历数组
for (NSDictionary * dic in self.datasArray) {
NSString * names = dic[@"country_name"];
switch ([dic[@"area_id"] integerValue]) {
case 1:
{
asiaCount+=1;
if (asiaCount ==4) {
asiaStr = [asiaStr stringByAppendingString:@"..."];
}else if (asiaCount <= 3){
if (asiaCount == 1) {
asiaStr = [NSString stringWithFormat:@"Aisa:%@",names];
}else{
asiaStr = [asiaStr stringByAppendingFormat:@"、%@",names];
}
}
}
break;
case 2:
{
europCount+=1;
if (europCount ==4) {
europStr = [europStr stringByAppendingString:@"..."];
}else if (europCount <= 3){
if (europCount == 1) {
europStr = [NSString stringWithFormat:@"Europe:%@",names];
}else{
europStr = [europStr stringByAppendingFormat:@"、%@",names];
}
}
}
break;
case 3:
{
southAmericaCount+=1;
if (southAmericaCount ==4) {
southAmericaStr = [southAmericaStr stringByAppendingString:@"..."];
}else if (southAmericaCount <= 3){
if (southAmericaCount == 1) {
southAmericaStr = [NSString stringWithFormat:@"Sourth America:%@",names];
}else{
southAmericaStr = [southAmericaStr stringByAppendingFormat:@"、%@",names];
}
}
}
break;
case 4:
{
oceanCount+=1;
if (oceanCount ==4) {
oceanStr = [oceanStr stringByAppendingString:@"..."];
}else if (oceanCount <= 3){
if (oceanCount == 1) {
oceanStr = [NSString stringWithFormat:@"Oceania:%@",names];
}else{
oceanStr = [oceanStr stringByAppendingFormat:@"、%@",names];
}
}
}
break;
case 5:
{
africaCount+=1;
if (africaCount ==4) {
africaStr = [africaStr stringByAppendingString:@"..."];
}else if (africaCount <= 3){
if (africaCount == 1) {
africaStr = [NSString stringWithFormat:@"Africa:%@",names];
}else{
africaStr = [africaStr stringByAppendingFormat:@"、%@",names];
}
}
}
break;
case 6:
{
northAmericaCount+=1;
if (northAmericaCount ==4) {
northAmericaStr = [northAmericaStr stringByAppendingString:@"..."];
}else if (northAmericaCount <= 3){
if (northAmericaCount == 1) {
northAmericaStr = [NSString stringWithFormat:@"North America:%@",names];
}else{
northAmericaStr = [northAmericaStr stringByAppendingFormat:@"、%@",names];
}
}
}
break;
}
}
4.条件判断,拼接字符串
if (asiaCount>0) {
countryName = asiaStr;
}
if (europCount>0) {
if (countryName.length>0) {
countryName = [countryName stringByAppendingFormat:@"\n%@",europStr];
}else{
countryName = europStr;
}
}
if (southAmericaCount>0) {
if (countryName.length>0) {
countryName = [countryName stringByAppendingFormat:@"\n%@",southAmericaStr];
}else{
countryName = southAmericaStr;
}
}
if (oceanCount>0) {
if (countryName.length>0) {
countryName = [countryName stringByAppendingFormat:@"\n%@",oceanStr];
}else{
countryName = oceanStr;
}
}
if (africaCount>0) {
if (countryName.length>0) {
countryName = [countryName stringByAppendingFormat:@"\n%@",africaStr];
}else{
countryName = africaStr;
}
}
if (northAmericaCount>0) {
if (countryName.length>0) {
countryName = [countryName stringByAppendingFormat:@"\n%@",northAmericaStr];
}else{
countryName = northAmericaStr;
}
}
最后打印countryName
Aisa:亚美尼亚、中国、日本...
Europe:奥地利、奥地利、奥地利...
Sourth America:乌拉圭、巴拉圭、阿根廷...
Oceania:澳大利亚、纽西兰、海盗...
Africa:非洲7、非洲6、非洲5...
North America:美国、加拿大、北美1...