我正在尝试创建这个方法。我们把它命名为
-(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到这个方法,然后需要检查一个plist文件,其中色相键为100个元素。我需要将我的色调与所有的色调进行比较,得到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;
}