iOS 快速查找数组中长度最长的字符串
- (NSString *)findArrMaxLengthOfStringArr:(NSArray<NSString *> *)arr {
NSMutableDictionary *lengthDic = [NSMutableDictionary dictionary];
NSMutableArray *strLengthArr = [NSMutableArray array];
NSInteger length = 0;
for (int i = 0; i < arr.count; i++) {
NSString *iStr = arr[i];
[lengthDic setObject:iStr forKey:@(iStr.length)];
[strLengthArr addObject:@(iStr.length)];
}
NSInteger max = [[strLengthArr valueForKeyPath:@"@max.floatValue"] integerValue];
NSString *value = [lengthDic objectForKey:@(max)];
return value;
}
网友评论