[英]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.