如何从NSMutableArray获取最大值。值存储为数组中的NSString,如下所示
for(int counter=0; counter<[datesArray count];counter++)
{
glass = [NSString stringWithFormat:@"%f",[[dbhandler todayData:currentDate] floatValue]];
[graphGlassesArray addObject:glass];
}
数组名称为graphGlassesArray
。
答案 0 :(得分:4)
您可以使用以下方法从数组中获取最大值:
NSNumber* max = [graphGlassesArray valueForKeyPath:@"max.self"];
float maxFloat = [max floatValue]
答案 1 :(得分:1)
您还应该尝试使用键值编码(https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/KeyValueCoding/Articles/CollectionOperators.html)。要获得NSArray中的最大值,您可以[array valueForKeyPath:@"@max.attribute"]
。请尝试查看此链接http://nshipster.com/kvc-collection-operators/。
答案 2 :(得分:0)
float max = 0.f;
for(int counter=0; counter<[datesArray count];counter++)
{
glass = [NSString stringWithFormat:@"%f",[[dbhandler todayData:currentDate] floatValue]];
[graphGlassesArray addObject:glass];
if(max < [[dbhandler todayData:currentDate] floatValue]) {
max = [[dbhandler todayData:currentDate] floatValue];
}
}
试试这个
答案 3 :(得分:0)
尝试排序可变数组
NSSortDescriptor *desc = [[NSSortDescriptor alloc] initWithKey:nil ascending:NO selector:@selector(localizedCompare:)];
NSString *maxValue = [[graphGlassesArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:desc]] objectAtIndex:0];
[desc release];