字符串、数组、字典的常用方法
目录
一、字符串
1.过滤掉地址中省份的方法(掌握string的方法)
2.富文本的使用
3.字符串相等的坑
4.身份证的*处理
5.字符串转换问题
6.字符串截取大总结
7.克数换算成价格
8.数字转String
9.两个字符串做运算
10.NSString 和 NSMutableAttributedString 如何相互转换
11.关于使用NSURL *url = [NSURL URLWithString:String];url为空的问题
12.iOS中使用正则表达式判断身份证格式及银行卡号格式是否正确
13.NSString字符串如何转为NSNumber
14.字符串判断为空
15.字符串去掉空格
16.判断字符串包含某个字符,字符串拆分成数组(去掉地址中“xx省”)
17.字符串文字上面加贯穿横线
18.字符串价格最多保留两位小数,00的取整数
19.字符串距离计算多少米/公里
20.bbx乘客端司机姓名更改成xx师傅二、数组
1.链表和数组的区别在哪里
2.数组去重
3.BBX个人中心数组初始化添加方式
4.从2个数组中取出元素拼接成新数组
5.数组 componentsSeparatedByString 与 componentsJoinedByString 方法
6.数组遍历大总结
7.找出数组中某个元素在数组中的位置顺序
8.遍历两个数组中的字典取出相同键的元素值(bbx乘客端智能调度需求 - 到xx城市下各个方位城市的时间)
9.bbx乘客端首页数组数据按照添加不同数组模型的顺序进行组装最后的数组三、字典
1.字典创建方法不同,为空的时候闪退
2.字典根据Value找Key重复和不重复的两种情况
一、字符串
1.过滤掉地址中省份的方法(掌握string的方法)
方法一:
positionCity.adress = @“四川省成都市武侯区成汉南路”;改后:成都市武侯区成汉南路
NSRange range = [result.address rangeOfString:@"省"];
if (range.length) {
range.length = range.location+range.length;
range.location = 0;
positionCity.adress = [result.address stringByReplacingCharactersInRange:range withString:@""];
} else {
positionCity.adress = result.address;
}
weakSelf.positionCity = positionCity;
weakSelf.travelRouteView.startLabel.text = positionCity.adress;
[weakSelf.travelRouteView.startLabel setTextColor:[UToColor blackColor]];
weakSelf.travelRouteView.positionCity = positionCity;
方法二:
在doNotWant这个字符集里想写几个过滤的字符就写几个
NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:@"市"];
_orderDetail.srcCity = [[_orderDetail.srcCity componentsSeparatedByCharactersInSet: doNotWant]componentsJoinedByString: @""];
_orderDetail.destCity = [[_orderDetail.destCity componentsSeparatedByCharactersInSet: doNotWant]componentsJoinedByString: @""];
cell.subLabel.text = [NSString stringWithFormat:@"%@ ⇀ %@",_orderDetail.srcCity,_orderDetail.destCity];
2.富文本的使用
NSString *text = [NSString stringWithFormat:@"¥%.2f/座 共%@座",[_priceText.text floatValue],_seatNum.text];
NSRange range = [text rangeOfString:[NSString stringWithFormat:@"%.2f",[_priceText.text floatValue]]];
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc]initWithString:text];
[attributeString setAttributes:@{NSForegroundColorAttributeName : [UToColor redColor],NSFontAttributeName : [UToFont subTitleFont]} range:range];
NSRange range2 = [text rangeOfString:[NSString stringWithFormat:@"/座 剩%@座",_seatNum.text]];
[attributeString setAttributes:@{NSForegroundColorAttributeName : [UToColor blackColor], NSFontAttributeName : [UToFont titleFont]} range:range2];
_orderSeatInfo.attributedText = attributeString;
NSString *text = [NSString stringWithFormat:@"费用:¥%.2f/座",[_orderDetail.seatPrice floatValue]];
NSRange range = [text rangeOfString:[NSString stringWithFormat:@"%.2f",[_orderDetail.seatPrice floatValue]]];
NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc]initWithString:text];
[attributedStr setAttributes:@{NSForegroundColorAttributeName : [UToColor redColor]} range:range];
NSRange range2 = [text rangeOfString:[NSString stringWithFormat:@"座"]];
[attributedStr setAttributes:@{NSForegroundColorAttributeName : [UToColor blackColor],NSFontAttributeName : [UIFont systemFontOfSize:11]} range:range2];
_tripOrderView.priceLabel.attributedText = attributedStr;
NSString *text = [NSString stringWithFormat:@"¥%.1f/座",_journey.seatPrice];
NSRange range = [text rangeOfString:[NSString stringWithFormat:@"%.1f",_journey.seatPrice]];
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:text];
[attributeString setAttributes:@{NSForegroundColorAttributeName : [UIColor redColor], NSFontAttributeName : [UToFont maxTitleFont]} range:range];
cell.rightLabel.attributedText = attributeString;
- 复杂的例子1如图:
- 复杂的例子2如图:
- 富文本给文字添加下划线
3.字符串相等的坑
isEqualToString 和 == 的区别,前面是正确的字符串比较相等方法,如:if([strA isEqualToString:strB]);后面的等号对比如:if ( strA == strB),这个strA, strB 是指针, 虽然字符串的内容是相同的, 但指向字符串的指针肯定是不同的,所以也不能相同啊。(为了更好地理解字符串,需要弄清楚指针的概念,内存的分配。 )
4.身份证的*处理
对身份证号码进行加*号处理,身份证号码主要有15、17、18三种
5.字符串转换问题
6.字符串截取大总结
1.截取字符串中两个指定字符串中间的字符串
// 要截取 "> 和 </ 之间的汉字内容:
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSString *string = @"<a href=\"http\">这是要截取的内容</a>";
NSRange startRange = [string rangeOfString:@"\">"];
NSRange endRange = [string rangeOfString:@"</"];
NSRange range = NSMakeRange(startRange.location + startRange.length, endRange.location - startRange.location - startRange.length);
NSString *result = [string substringWithRange:range];
NSLog(@"%@",result);
}
@end
补充:如果要截取字符串中较多这样的相同的两个字符之间的字符串,需要循环截取;链接:https://blog.csdn.net/IOS_TXQ/article/details/52119880
2.根据特定字符将字符串一分为二
7.克数换算成价格
8.数字转String
UITextField填写的数是字符串转成数字类型,然后提交参数的时候需要再转成字符串
9.两个字符串做运算
注意如果两个字符串进行运算或者大小比较,需要转成 doubleValue,尽量不要转成 intValue 和 integerValue,因为当字符串数字太小的时候比如0.1转成 intValue 和 integerValue 就变成0,会产生业务 bug。
10.NSString 和 NSMutableAttributedString 如何相互转换
//NSString to NSMutableAttributedString
NSString *aString=@"Hello, world!";
NSMutableAttributedString *attributedString=[[NSMutableAttributedString alloc] initWithString:aString attributes:nil];
[attributedString setAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]} range:NSMakeRange(0, 5)];
//NSMutableAttributedString to NSString
NSString *anotherString=[attributedString string];
11.关于使用NSURL *url = [NSURL URLWithString:String];url为空的问题
NSURL *url = [NSURL URLWithString:String];一直返回为空,这是有的链接需要转换一下utf8才能正常加载,有的不需要转换直接可以加载,所以一起直接转换再加载比较稳妥。
urlString = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:urlString];
12.iOS中使用正则表达式判断身份证格式及银行卡号格式是否正确
http://www.weixuecn.cn/article/12372.html
https://www.2cto.com/kf/201311/256494.html
13.NSString字符串如何转为NSNumber
14.字符串判断为空
15.字符串去掉空格
1.使用 NSString中的stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] 方法只是去掉左右两边的空格;
2.使用 NSString *strUrl = [urlString stringByReplacingOccurrencesOfString:@" " withString:@""]; 可以去掉空格,注意此时生成的 strUrl 是 autorelease 属性的,不要妄想对 strUrl 进行 release 操作。
- (void)setEndpointModel:(BBXHomeRecEndpointModel *)endpointModel {
_endpointModel = endpointModel;
//end_address 比如:@“浙江省 杭州市萧山机场“
NSString *endAdressStr = endpointModel.end_address;
if ([endpointModel.end_address containsString:@"省"] && endpointModel.order_type == 4) {//endpointModel.end_province_name 快线
//可以去掉空格(变成:@“浙江省杭州市萧山机场“)
NSString *adressNoKongStr = [endpointModel.end_address stringByReplacingOccurrencesOfString:@" " withString:@""];
NSArray *array = [adressNoKongStr componentsSeparatedByString:@"省"];
endAdressStr = array.lastObject;
}
[self.adressLB rz_colorfulConfer:^(RZColorfulConferrer * _Nonnull confer) {
confer.text(@"到").font(kFont_Medium(16)).textColor(HEXCOLOR(0x808BB9));
confer.text(@" ");
confer.text(endAdressStr).font(kFont_Medium(16)).textColor(BBXBlack);
}];
}
16.判断字符串包含某个字符,字符串拆分成数组(去掉地址中“xx省”)
相关链接:
https://blog.csdn.net/lurenjia_KB/article/details/65630185
https://codeleading.com/article/6415647192/
17.字符串文字上面加贯穿横线
- (UILabel *)originalPriceLB {
if (!_originalPriceLB) {
_originalPriceLB = [[UILabel alloc]init];
_originalPriceLB.font = [UIFont systemFontOfSize:11];
_originalPriceLB.backgroundColor = [UIColor clearColor];
_originalPriceLB.textColor = BBXGray;
_originalPriceLB.textAlignment = NSTextAlignmentRight;
[self.cellBgView addSubview: _originalPriceLB];
NSMutableAttributedString *newPrice = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@",@"¥90"]];
//划横线
[newPrice addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:NSMakeRange(0, newPrice.length)];
_originalPriceLB.attributedText = newPrice;
_originalPriceLB.hidden = YES;
}
return _originalPriceLB;
}
18.字符串价格最多保留两位小数,00的取整数
价格需求正确如:¥23.01、¥23.10、¥23,不能为¥23.00和¥23.101等。
@property (nonatomic , copy) NSString * price;//endpointModel.recommend.type_1.price 为字符串
//处理价格
- (void) priceHandel {
//价格
if (endpointModel.recommend.type_1.price.length>0) {
NSString *priceStr = [NSString stringWithFormat:@"%.2f",[endpointModel.recommend.type_1.price doubleValue]/100];
self.botPriceLB.text = [NSString stringWithFormat:@"¥%@起",[self removeSuffix:priceStr]];
} else {
[self.botTakeTypeLB mas_remakeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(15);
make.centerY.mas_equalTo(self.bottomTakeBgview);
make.height.mas_equalTo(21);
make.width.mas_equalTo(33);
}];
}
}
//对于.2f格式化的字符串,如果小数点为00则取整
- (NSString *)removeSuffix:(NSString *)numberStr {
if (numberStr.length > 1) {
if ([numberStr componentsSeparatedByString:@"."].count == 2) {
NSString *last = [numberStr componentsSeparatedByString:@"."].lastObject;
if ([last isEqualToString:@"00"]) {
numberStr = [numberStr substringToIndex:numberStr.length - (last.length + 1)];
return numberStr;
} else {
// if ([[last substringFromIndex:last.length -1] isEqualToString:@"0"]) {//去除末尾0
// numberStr = [numberStr substringToIndex:numberStr.length - 1];
// return numberStr;
// }
return numberStr;
}
}
return numberStr;
} else {
return nil;
}
}
19.字符串距离计算多少米/公里
后台接口返回距离字段一般单位都是米,需要自己根据需求进行计算。
//快线订单 - 获取距离上下车站点距离
@property (nonatomic , copy) NSString * distance;//米
//计算距离多少
- (void)distanceHandel {
//上车点
NSString *onString = @"";
NSString *startAdress = @"";
if (kStrEqual(travelwayModel.next_flight.oncar_area_level, @"1")) {//0取站点里数据,1取外层
startAdress = self.start.detailAddress;//取当前选择起点
onString = @" (上门接客)";
} else {
startAdress = travelwayModel.oncar_station_name;
onString = [self p_setDistanceResult:self.oncarModel.distance];
}
self.fastlineStartPointLB.text = [NSString stringWithFormat:@"上车站点:%@%@",startAdress,onString];
}
//处理距离
- (NSString *)p_setDistanceResult:(NSString *)distance {
NSString *distanceString = @"";
CGFloat distanceFloat = [distance floatValue];
if (distanceFloat > 1000) {
CGFloat distanceResult = distanceFloat / 1000;
distanceString = [NSString stringWithFormat:@" (约%.1f公里)",distanceResult];
}else{
distanceString = [NSString stringWithFormat:@" (约%ld米)",(long)[distance integerValue]];
}
return distanceString;
}
20.bbx乘客端司机姓名更改成xx师傅
//司机名字处理成xx师傅
- (void)drivernameHandel {
//@property (nonatomic , copy) NSString * driver_name;//司机姓名
NSString *driver_name = [MacroAppInfo getDriverUserName:travelwayModel.tcpSuggestlist.driver_info.driver_name];//司机姓名处理(姓+师傅)
self.driverLB.text = driver_name;
NSString *carClassName = travelwayModel.tcpSuggestlist.driver_info.car_info.car_class_name;
if ([carClassName containsString:@"/"]) {
NSArray *array = [carClassName componentsSeparatedByString:@"/"];
carClassName = array.firstObject;
}
NSString *yearStr = [NSString stringWithFormat:@" | %@年驾龄",travelwayModel.tcpSuggestlist.driver_info.driving_years];
NSString *starStr = [NSString stringWithFormat:@" | %.1f分",[travelwayModel.tcpSuggestlist.star doubleValue]/20];
self.carInfoLB.text = [NSString stringWithFormat:@"%@%@%@",carClassName?:@"",travelwayModel.tcpSuggestlist.driver_info.driving_years?yearStr:@"",travelwayModel.tcpSuggestlist.star?starStr:@""];
}
MacroAppInfo 类
/**
* 获取司机的名字 性 + 师傅
*/
+(NSString *)getDriverUserName:(NSString *)userName
{
return [NSString stringWithFormat:@"%@师傅",[self getUserNameFirstChar:userName]];
}
+ (NSString *)getUserNameFirstChar:(NSString *)userName
{
NSArray * suernameS = [self getSurnameS];
if(userName.length > 0)
{
for(NSString * subN in suernameS)
{
if([userName hasPrefix:subN])
{
return subN;
}
}
return [userName substringToIndex:1];
}
return @"";
}
+ (NSArray *)getSurnameS
{
return @[@"欧阳",@"太史",@"端木",@"上官",@"司马",@"东方",@"独孤",@"南宫",@"万俟",@"闻人",@"夏侯",@"诸葛",@"尉迟",@"公羊",@"赫连",@"澹台",@"皇甫",@"宗政",@"濮阳",@"公冶",@"太叔",@"申屠",@"公孙",@"慕容",@"仲孙",@"钟离",@"长孙",@"宇文",@"司徒",@"鲜于",@"司空",@"闾丘",@"子车",@"亓官",@"司寇",@"巫马",@"公西",@"颛孙",@"壤驷",@"公良",@"漆雕",@"乐正",@"宰父",@"谷梁",@"拓跋",@"夹谷",@"轩辕",@"令狐",@"段干",@"百里",@"呼延",@"东郭",@"南门",@"羊舌",@"微生",@"公户",@"公玉",@"公仪",@"梁丘",@"公仲",@"公上",@"公门",@"公山",@"公坚",@"左丘",@"公伯",@"西门",@"公祖",@"第五",@"公乘",@"贯丘",@"公皙",@"南荣",@"东里",@"东宫",@"仲长",@"子书",@"子桑",@"即墨",@"达奚",@"褚师",@"吴铭"];
}
二、数组
1.链表和数组的区别在哪里
二者都属于一种数据结构
- 从逻辑结构来看:
- 数组必须事先定义固定的长度(元素个数),不能适应数据动态地增减的情况。当数据增加时,可能超出原先定义的元素个数;当数据减少时,造成内存浪费;数组可以根据下标直接存取。
- 链表动态地进行存储分配,可以适应数据动态地增减的情况,且可以方便地插入、删除数据项。(数组中插入、删除数据项时,需要移动其它数据项,非常繁琐)链表必须根据next指针找到下一个元素
- 从内存存储来看:
- (静态)数组从栈中分配空间, 对于程序员方便快速,但是自由度小
- 链表从堆中分配空间, 自由度大但是申请管理比较麻烦
从上面的比较可以看出,如果需要快速访问数据,很少或不插入和删除元素,就应该用数组;相反, 如果需要经常插入和删除元素就需要用链表数据结构了。
2.数组去重
3.BBX个人中心数组初始化添加方式
多个分组的数组数据可以以下2种方式进行本地构建,效果如下。
方法1:
- (NSMutableArray *)dataArray {
if (!_dataArray) {
_dataArray = [[NSMutableArray alloc]init];
[_dataArray addObject:@[ ]];
[_dataArray addObject:@[@{@"title":@"消息中心",@"info":@"",@"ctrl":@"MessageVC"}]];
if ([BBXDriverInfo shareManager].show_payment) {//显示我的缴费入口
if (([BBXDriverInfo shareManager].is_citic == 1 && [BBXDriverInfo shareManager].payment_account_type == 3) || [BBXDriverInfo shareManager].citic_charge == YES) {//中信缴费入口
if ([BBXDriverInfo shareManager].fee_splitting_config == 1) {//中信我的分润入口
[_dataArray addObject:@[
@{@"title":@"可提现账户",@"info":@"",@"ctrl":@"BBXAccountViewController",@"type":@(AccountTypeWithdraw)},
@{@"title":@"欠费账户",@"info":@"",@"ctrl":@"BBXAccountViewController",@"type":@(AccountTypeOwe)},
@{@"title":@"我的缴费(中信)",@"info":@"",@"ctrl":@"BBXZhongxinPayRecordViewController"},
@{@"title":@"我的分润(中信)",@"info":@"",@"ctrl":@"BBXShareProfitRecordViewController"},
@{@"title":@"历史订单",@"info":@"",@"ctrl":@"BBXHistoryOrderViewController"},
@{@"title":@"我的缴费",@"info":@"",@"ctrl":@"BBXPaymentRecordViewController"},
]];
} else {
[_dataArray addObject:@[
@{@"title":@"可提现账户",@"info":@"",@"ctrl":@"BBXAccountViewController",@"type":@(AccountTypeWithdraw)},
@{@"title":@"欠费账户",@"info":@"",@"ctrl":@"BBXAccountViewController",@"type":@(AccountTypeOwe)},
@{@"title":@"我的缴费(中信)",@"info":@"",@"ctrl":@"BBXZhongxinPayRecordViewController"},
@{@"title":@"历史订单",@"info":@"",@"ctrl":@"BBXHistoryOrderViewController"},
@{@"title":@"我的缴费",@"info":@"",@"ctrl":@"BBXPaymentRecordViewController"},
]];
}
} else {
if ([BBXDriverInfo shareManager].fee_splitting_config == 1) {//中信我的分润入口
[_dataArray addObject:@[
@{@"title":@"可提现账户",@"info":@"",@"ctrl":@"BBXAccountViewController",@"type":@(AccountTypeWithdraw)},
@{@"title":@"欠费账户",@"info":@"",@"ctrl":@"BBXAccountViewController",@"type":@(AccountTypeOwe)},
@{@"title":@"我的分润(中信)",@"info":@"",@"ctrl":@"BBXShareProfitRecordViewController"},
@{@"title":@"历史订单",@"info":@"",@"ctrl":@"BBXHistoryOrderViewController"},
@{@"title":@"我的缴费",@"info":@"",@"ctrl":@"BBXPaymentRecordViewController"},
]];
} else {
[_dataArray addObject:@[
@{@"title":@"可提现账户",@"info":@"",@"ctrl":@"BBXAccountViewController",@"type":@(AccountTypeWithdraw)},
@{@"title":@"欠费账户",@"info":@"",@"ctrl":@"BBXAccountViewController",@"type":@(AccountTypeOwe)},
@{@"title":@"历史订单",@"info":@"",@"ctrl":@"BBXHistoryOrderViewController"},
@{@"title":@"我的缴费",@"info":@"",@"ctrl":@"BBXPaymentRecordViewController"},
]];
}
}
} else {//不显示我的缴费入口
if (([BBXDriverInfo shareManager].is_citic == 1 && [BBXDriverInfo shareManager].payment_account_type == 3) || [BBXDriverInfo shareManager].citic_charge == YES) {//中信缴费入口
if ([BBXDriverInfo shareManager].fee_splitting_config == 1) {//中信我的分润入口
[_dataArray addObject:@[
@{@"title":@"可提现账户",@"info":@"",@"ctrl":@"BBXAccountViewController",@"type":@(AccountTypeWithdraw)},
@{@"title":@"欠费账户",@"info":@"",@"ctrl":@"BBXAccountViewController",@"type":@(AccountTypeOwe)},
@{@"title":@"我的缴费(中信)",@"info":@"",@"ctrl":@"BBXZhongxinPayRecordViewController"},
@{@"title":@"我的分润(中信)",@"info":@"",@"ctrl":@"BBXShareProfitRecordViewController"},
@{@"title":@"历史订单",@"info":@"",@"ctrl":@"BBXHistoryOrderViewController"},
]];
} else {
[_dataArray addObject:@[
@{@"title":@"可提现账户",@"info":@"",@"ctrl":@"BBXAccountViewController",@"type":@(AccountTypeWithdraw)},
@{@"title":@"欠费账户",@"info":@"",@"ctrl":@"BBXAccountViewController",@"type":@(AccountTypeOwe)},
@{@"title":@"我的缴费(中信)",@"info":@"",@"ctrl":@"BBXZhongxinPayRecordViewController"},
@{@"title":@"历史订单",@"info":@"",@"ctrl":@"BBXHistoryOrderViewController"},
]];
}
} else {
if ([BBXDriverInfo shareManager].fee_splitting_config == 1) {//中信我的分润入口
[_dataArray addObject:@[
@{@"title":@"可提现账户",@"info":@"",@"ctrl":@"BBXAccountViewController",@"type":@(AccountTypeWithdraw)},
@{@"title":@"欠费账户",@"info":@"",@"ctrl":@"BBXAccountViewController",@"type":@(AccountTypeOwe)},
@{@"title":@"我的分润(中信)",@"info":@"",@"ctrl":@"BBXShareProfitRecordViewController"},
@{@"title":@"历史订单",@"info":@"",@"ctrl":@"BBXHistoryOrderViewController"},
]];
} else {
[_dataArray addObject:@[
@{@"title":@"可提现账户",@"info":@"",@"ctrl":@"BBXAccountViewController",@"type":@(AccountTypeWithdraw)},
@{@"title":@"欠费账户",@"info":@"",@"ctrl":@"BBXAccountViewController",@"type":@(AccountTypeOwe)},
@{@"title":@"历史订单",@"info":@"",@"ctrl":@"BBXHistoryOrderViewController"},
]];
}
}
}
[_dataArray addObject:@[
@{@"title":@"我的购票码",@"info":@"",@"ctrl":@"BBXBusTicketPurchaseCodeViewController"},
@{@"title":@"我的设置",@"info":@"",@"ctrl":@"SettingVC"},
@{@"title":@"联系客服",@"info":@"95170",@"ctrl":@"PhoneCall"},
@{@"title":@"邀请有礼",@"info":@"",@"ctrl":@"BBXInviteDriverViewController"},
]];
}
return _dataArray;
}
方法2:
- (NSMutableArray *)dataArray {
if (!_dataArray) {
_dataArray = [[NSMutableArray alloc]init];
NSMutableArray *sectionOneArr = [[NSMutableArray alloc]init];
NSMutableArray *sectionTwoArr = [[NSMutableArray alloc]init];
NSMutableArray *sectionThreeArr = [[NSMutableArray alloc]init];
[sectionOneArr addObject:@{@"title":@"消息中心",@"info":@"",@"ctrl":@"MessageVC"}];
[sectionTwoArr addObject:@{@"title":@"可提现账户",@"info":@"",@"ctrl":@"BBXAccountViewController",@"type":@(AccountTypeWithdraw)}];
[sectionTwoArr addObject:@{@"title":@"欠费账户",@"info":@"",@"ctrl":@"BBXAccountViewController",@"type":@(AccountTypeOwe)}];
if ([BBXDriverInfo shareManager].citic_charge == YES) {//中信缴费入口
[sectionTwoArr addObject:@{@"title":@"我的缴费(中信)",@"info":@"",@"ctrl":@"BBXZhongxinPayRecordViewController"}];
}
if ([BBXDriverInfo shareManager].fee_splitting_config == 1) {//中信我的分润入口
[sectionTwoArr addObject:@{@"title":@"我的分润(中信)",@"info":@"",@"ctrl":@"BBXShareProfitRecordViewController"}];
}
[sectionTwoArr addObject:@{@"title":@"历史订单",@"info":@"",@"ctrl":@"BBXHistoryOrderViewController"}];
if ([BBXDriverInfo shareManager].show_payment) {//显示我的缴费入口
[sectionTwoArr addObject:@{@"title":@"我的缴费",@"info":@"",@"ctrl":@"BBXPaymentRecordViewController"}];
}
if ([BBXDriverInfo shareManager].citic_car_charge == 1) {//车辆缴费入口
[sectionTwoArr addObject:@{@"title":@"车辆缴费",@"info":@"",@"ctrl":@"BBXZhongxinPayRecordViewController"}];
}
[sectionThreeArr addObject:@{@"title":@"我的购票码",@"info":@"",@"ctrl":@"BBXBusTicketPurchaseCodeViewController"}];
[sectionThreeArr addObject:@{@"title":@"我的设置",@"info":@"",@"ctrl":@"SettingVC"}];
[sectionThreeArr addObject:@{@"title":@"联系客服",@"info":@"95170",@"ctrl":@"PhoneCall"}];
[sectionThreeArr addObject:@{@"title":@"邀请有礼",@"info":@"",@"ctrl":@"BBXInviteDriverViewController"}];
[_dataArray addObject:sectionOneArr];
[_dataArray addObject:sectionTwoArr];
[_dataArray addObject:sectionThreeArr];
}
return _dataArray;
}
//其他地方调用
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSDictionary *dict = self.dataArray[indexPath.section][indexPath.row];
if ([dict[@"ctrl"] isEqualToString:@"PhoneCall"]) {
[CJCallUtil callPhoneWithNum:dict[@"info"] atView:self.view];
}
else if ([dict[@"ctrl"] isEqualToString:@"BBXAccountViewController"]) {
AccountType type = (AccountType)[dict[@"type"] intValue];
BBXAccountViewController *vc = [[BBXAccountViewController alloc] initWithAccountType:type];
[self.navigationController pushViewController:vc animated:YES];
}
else if ([dict[@"ctrl"] isEqualToString:@"BBXPaymentRecordViewController"]) {
BBXPaymentRecordViewController *vc = [[BBXPaymentRecordViewController alloc] init];
[self.navigationController pushViewController:vc animated:YES];
}
else if ([dict[@"ctrl"] isEqualToString:@"BBXHistoryOrderViewController"]) {//历史订单
BBXHistoryOrderViewController *vc = [[BBXHistoryOrderViewController alloc] init];
[self.navigationController pushViewController:vc animated:YES];
}
else if ([dict[@"ctrl"] isEqualToString:@"BBXZhongxinPayRecordViewController"]) {//中信缴费入口、车辆缴费
BBXZhongxinPayRecordViewController *vc = [[BBXZhongxinPayRecordViewController alloc] init];
if ([dict[@"title"] isEqualToString:@"车辆缴费"]) {
vc.isVehiclePayment = YES;
} else {
vc.isVehiclePayment = NO;
}
[self.navigationController pushViewController:vc animated:YES];
}
else if ([dict[@"ctrl"] isEqualToString:@"MessageVC"]) {//消息中心
[self clickMessageEvent:NO];
MessageVC *VC = [[MessageVC alloc] init];
[self.navigationController pushViewController:VC animated:YES];
}
else if ([dict[@"ctrl"] isEqualToString:@"BBXInviteDriverViewController"]) {//招募
BBXInviteDriverViewController *VC = [[BBXInviteDriverViewController alloc] init];
VC.urlString = [BBXURL urlDriver_Invite];
[self.navigationController pushViewController:VC animated:YES];
}
else {
Class cls = NSClassFromString(dict[@"ctrl"]);
UIViewController *vc = [[cls alloc] init];
if ([vc isKindOfClass:[UIViewController class]]) {
[self.navigationController pushViewController:vc animated:YES];
}
}
}
4.从2个数组中取出元素拼接成新数组
问题场景:
后台传回来2个数组,时间周期数组loancycle和利率数组loanrate,现在需要将他们拼接成一个数组然后按照下图所示格式来展示(分几期 利率多少)
5.从数组中(每个元素由多个元素组成)取出一个想要元素组成数组
5.数组 componentsSeparatedByString 与 componentsJoinedByString 方法
1.componentsSeparatedByString
将string字符串转换为array数组;
NSArray *array = [Str componentsSeparatedByString:@","];注意://componentsSeparatedByString 这个方法有一个bug 当被切割的字符串是 @“” 时,切割后返回的数组长度为1 元素为 @“”
例子:比如下面的transitStep.polyline为字符串,通过;分成数组,再通过,再分成数组。
2.componentsJoinedByString
将mutableArray数组转换为string字符串;
NSString *tempString = [mutableArray componentsJoinedByString:@","]; --分隔符
6.数组遍历大总结
1.for;这个方法最普通效率一般,一般用于针对下标的处理和反向遍历
2.for in;快速遍历,效率最高
3.enumerateObjectsUsingBlock;block里面的参数包括object,下标以及是否停止遍历,应该说这个能满足基本所有的遍历需求。有下标和运行的对象,还有是否继续遍历的标志。不过反向遍历呢?苹果提供了另外一个方法:enumerateObjectsWithOptions:NSEnumerationReverse usingBlock;效率不咋样常见遍历方法比较:http://t.zoukankan.com/tangyuanby2-p-7150103.html
遍历性能比较:https://blog.csdn.net/songzhuo1991/article/details/116012426
1.for循环
- (void)setModel:(OrderListModel *)model {
_model = model;
if (model.order_status == Order_state_sended) {//接客
if (model.on_seq == 0 || !model.on_seq) {
self.pinBgView.image = kImage(@"bbxMapPeople");
} else {
for (int i = 0; i<self.pickArray.count; i++) {
if (model.order_id == [self.pickArray[i] order_id]) {//唯一id标识符,当前订单
self.numLb.text = [NSString stringWithFormat:@"%d",i+1];
self.pinBgView.image = kImage(@"bbxMapPoint");
break;//跳出循环
}
}
}
} else {
self.numLb.text = @"";
if (self.isPickMapChuKou == YES) {//接客
self.pinBgView.image = kImage(@"bbxMapExit");//出口
} else {
self.pinBgView.image = kImage(@"bbxMapEnter");//入口
}
}
}
2.for in循环
NSMutableArray *temArray = [NSMutableArray array];
for (OrderListModel *model in self.orderArray) {
if (model.order_status == Order_state_sended && model.order_status != Order_state_get_on && !(model.order_origin.intValue == 999)) {
[temArray addObject:model];
}
}
self.clusterManager.dataArray = temArray;//聚合
3.enumerateObjectsUsingBlock
__block NSMutableArray * list_jijia = [[NSMutableArray alloc] init];
__block NSMutableArray * list_normal = [[NSMutableArray alloc] init];
[self.orderArray enumerateObjectsUsingBlock:^(OrderListModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if(obj.calc_type != 0) {
[list_jijia addObject:obj];
} else {
[list_normal addObject:obj];
}
}];
7.找出数组中某个元素在数组中的位置顺序
https://blog.csdn.net/ios_san_diego/article/details/72021173
举例1:
举例2:
//找到数组中所在的位置
- (void)lookArrayIndex {
// @property (nonatomic , copy) NSArray<NSString *> * start_sequnence;//当前车次-找对应上车时间
// @property (nonatomic , copy) NSString * left_delay;//上车时间2
// @property (nonatomic , copy) NSArray<NSString *> * on_times;//上车时间1
//on_times上车时间怎么取?取数组里面哪一个是根据start_sequnence数组里面字符串@"VID"所在的顺序来取on_times数组中的相对应顺序的字符串时间
//处理接驾时间 - 已知当前某个时间和延迟多少秒,计算延迟后具体是什么时间点
NSInteger index = 0;
for (NSString *sequnence in travelwayModel.tcpSuggestlist.start_sequnence) {
if (kStrEqual(sequnence, @"VID")) {
index = [travelwayModel.tcpSuggestlist.start_sequnence indexOfObject:sequnence];//找到VID在数组中的所在位置
}
}
NSDateFormatter *dataFormatter = [[NSDateFormatter alloc]init];
dataFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
[dataFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"Asia/Shanghai"]];
NSString *onTimeStr = travelwayModel.tcpSuggestlist.on_times[index];//2023-07-19 15:53:11
NSDate *ontimeDate = [dataFormatter dateFromString:onTimeStr];
NSTimeInterval ontimeInterval = [ontimeDate timeIntervalSince1970];//将时间字符串转时间戳(中间先转NSDate),这个也是精确到秒
NSTimeInterval lateInterval = ontimeInterval+[travelwayModel.tcpSuggestlist.left_delay doubleValue];//left_delay返回为秒
NSDate *lateDate = [NSDate dateWithTimeIntervalSince1970:lateInterval];
NSString *lateTimeStr = [dataFormatter stringFromDate:lateDate];//将时间戳转时间字符串(中间先转NSDate)
//获取时间字符串中的几点
if (lateTimeStr.length>=19) {
NSString *jiejiaTime = [lateTimeStr substringWithRange:NSMakeRange(11, 5)];
[self.taketimeLB rz_colorfulConfer:^(RZColorfulConferrer * _Nonnull confer) {
confer.text(@"预计").font(kFont(10)).textColor(BBXGray);
confer.text([NSString stringWithFormat:@"%@",jiejiaTime]).font(kFont(15)).textColor(BBXBlack);
confer.text(@"前接驾").font(kFont(10)).textColor(BBXGray);
}];
} else {
}
}
8.遍历两个数组中的字典取出相同键的元素值(bbx乘客端智能调度需求 - 到xx城市下各个方位城市的时间)
需求:根据接口请求返回当前位置起点可到达的城市以及城市下的各个线路的相关数据,但是不包含起点到各个线路所需要的最快送达时间,这时间获取通过传入线路id数组(由前面接口返回)去请求另外接口来获取时间相关数据(是个数组,每个数组里面包含字典,字典里面有线路id和时间字段),再根据线路id进行找到相对应线路的最快到达时间,再更新之前的数据进行展示界面,这个更新之前的数组很重要,因为没有更新这个操作导致页面一直刷新有问题,还以为是循环遍历进行线路id赋值方法有错,而实际上是最后没有更新之前数组的原因。
接口返回数据 - 获取线路列表时间即到xx线路最快多少时间到达:
{
message = ok;
result = {
list = (
{
duration = 0;
"line_id" = "361000_to_3100001";
},
{
duration = 0;
"line_id" = "361000_to_362300";
},
{
duration = "810.2";
"line_id" = "361000_to_361000";
},
{
duration = 0;
"line_id" = "361000_to_363900";
},
{
duration = 0;
"line_id" = "361000_to_362600";
},
{
duration = 0;
"line_id" = "361000_to_363300";
},
{
duration = 0;
"line_id" = "361000_to_366100";
},
{
duration = 0;
"line_id" = "361000_to_363600";
},
{
duration = 0;
"line_id" = "361000_to_363801";
},
{
duration = "3031.5";
"line_id" = "361000_to_363000";
},
{
duration = 0;
"line_id" = "361000_to_350120";
},
{
duration = 0;
"line_id" = "361000_to_363100";
},
{
duration = "4220.1";
"line_id" = "361000_to_362400";
}
);
};
status = 0;
}
因为没有进行最关键的先创建临时数组用于线路id时间匹配再对原先数组赋值,所以造成UI展示各种错误的错误写法:
9.bbx乘客端首页数组数据按照添加不同数组模型的顺序进行组装最后的数组
bbx乘客端首页数组数据按照添加不同数组模型的顺序进行组装最后的数组sectionArray,其顺序为:起终点、顺路车或者推荐卡片、顺路车或者推荐卡片2、到xx线路城市、广告banner、剩余到xx线路城市。其中如果某一项没有则其他项就顶替上去以此类推。在不同section的cell的数量、高度、具体数据值等都是通过这个sectionArray进行判断各自对应的模型是啥来处理,这边起终点用的是数组,一般正常最好都改成模型比较好,也就是每个都有一个模型属性这样去区分处理的时候也比较方便,这边因为就一个起终点数组还好,如果还有其他多个数组就不太好判断处理,需要特别注意。
三、字典
1.字典创建方法不同,为空的时候闪退
字典创建为空或者插入空数据nil会闪退。
相关链接:https://blog.csdn.net/u010707262/article/details/102801566
2.字典根据Value找Key重复和不重复的两种情况
https://blog.csdn.net/sinat_28709097/article/details/46821445