截屏2021-03-03 下午1.54.54.png
1.hi1 hello2 hello2 hi1
2.
方法:
- (CGFloat)getLeastFromArray:(NSArray *)array {
NSMutableArray *minArr = [NSMutableArray arrayWithCapacity:0];
for (int i = 0; i<array.count; i++) {
NSString *dateStr = array[i];
/// 1.先将时间转换为分钟
NSArray *arr = [dateStr componentsSeparatedByString:@":"];
NSInteger min = [arr.firstObject integerValue] * 60 + [arr.lastObject integerValue];
[minArr addObject:@(min)];
}
NSMutableArray *differentialArr = [NSMutableArray arrayWithCapacity:0];
for (int i = 0; i<minArr.count-1; i++) {
NSInteger min = [minArr[i] integerValue];
NSMutableArray *differentialArr1 = [NSMutableArray arrayWithCapacity:0];
for (int j = i+1; j<minArr.count; j++) {
NSInteger differential1 = labs(min - [minArr[j] integerValue]);
NSInteger differential2 = 24*60 - differential1;
[differentialArr1 addObject:@(MIN(differential2, differential1))];
}
[differentialArr addObject:@([[differentialArr1 valueForKeyPath:@"@min.floatValue"] floatValue])];
}
CGFloat least = [[differentialArr valueForKeyPath:@"@min.floatValue"] floatValue];
return least;
}
调用:
[self getLeastFromArray:@[@"23:30", @"22:01", @"16:30", @"05:00",@"20:05"]];
3.
方法:
- (void)setInfo:(NSDictionary *)info ForKey:(NSString *)key {
[[NSUserDefaults standardUserDefaults] setObject:info forKey:key];
[[NSUserDefaults standardUserDefaults] synchronize];
}
调用:
[self setInfo:json ForKey:@"getUserById"];
4.
方法:
- (NSString *)getMaxWithFigure:(NSString *)figure {
NSMutableArray *arr = [NSMutableArray arrayWithCapacity:0];
for (int i = 0; i<figure.length; i++) {
NSString *figureItem = [figure substringWithRange:NSMakeRange(i, 1)];
[arr addObject:figureItem];
}
NSString *maxStr = [NSString stringWithFormat:@"%@",[arr valueForKeyPath:@"@max.floatValue"]];
NSInteger index = [arr indexOfObject:maxStr];
[arr exchangeObjectAtIndex:0 withObjectAtIndex:index];
NSMutableString *figureStr = [[NSMutableString alloc] init];
[arr enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
[figureStr appendString:(NSString *)obj];
}];
return figureStr;
}
调用:
[self getMaxWithFigure:@"1234"]
网友评论