开发者

returning 8 closest cgfloat from a table lookup based on a cgfloat

I am trying to create this method. Let's call this

-(NSMutableAr开发者_JAVA技巧ray*) 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;

}

pretty much, I am passing a cgfloat to this method which then needs to check a plist file which have hue key for 100 elements. I need to compare my hue with all of the hues and get 8 most closest hue and finally wrap these into an array and return this.

What would be most efficient way of doing this? Thanks in advance.


Here's my method if anyone is interested. Feel free to comment on it.

-(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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜