[英]returning 8 closest cgfloat from a table lookup based on a cgfloat
我正在尝试创建此方法。 叫这个
-(NSMutableArray*) getEightClosestSwatchesFor:(CGFloat)hue
{
NSString *myFile = [[NSBundle mainBundle] pathForResource:@"festival101" ofType:@"plist"];
NSMutableArray* myArray = [NSArray arrayWithContentsOfFile:myFile];
for (NSDictionary *dict in myArray)
{
NSLog(@"[plistData valueForKey:aKey] string] is %f", [[dict valueForKey:@"hue"] floatValue]) ;
}
return myArray;
}
差不多,我正在将cgfloat传递给此方法,然后需要检查具有100个元素的色相键的plist文件。 我需要将我的色相与所有色相进行比较,并获得8个最接近的色相,最后将它们包装到数组中并返回。
什么是最有效的方法? 提前致谢。
如果有人感兴趣,这是我的方法。 随时对此发表评论。
-(NSArray*)eightClosestSwatchesForHue:(CGFloat)hue
{
NSMutableArray *updatedArray = [[NSMutableArray alloc] initWithCapacity:100];
NSString *myFile = [[NSBundle mainBundle] pathForResource:@"festival101" ofType:@"plist"];
NSMutableArray* myArray = [NSArray arrayWithContentsOfFile:myFile];
for (NSDictionary *dict in myArray)
{
CGFloat differenceHue = fabs(hue - [[dict valueForKey:@"hue"] floatValue]);
//create a KVA for the differenceHue here and then add it to the dictionary and add this dictionary to the array.
NSDictionary* tempDict = [NSDictionary dictionaryWithObjectsAndKeys:
[dict valueForKey:@"id"], @"id",
[NSNumber numberWithFloat:differenceHue], @"differenceHue",
[dict valueForKey:@"color"], @"color",
nil];
[updatedArray addObject:tempDict];
}
//now we have an array of dictioneries with values we want. we need to sort this from little to big now.
NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"differenceHue" ascending:YES];
[updatedArray sortUsingDescriptors:[NSArray arrayWithObjects:descriptor,nil]];
[descriptor release];
//now get the first 8 elements and get rid of the remaining.
NSArray *finalArray = [updatedArray subarrayWithRange:NSMakeRange(0,8)];
[updatedArray release];
return finalArray;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.