首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >根据cgfloat从查表中返回8个最接近的cgfloat

根据cgfloat从查表中返回8个最接近的cgfloat
EN

Stack Overflow用户
提问于 2011-07-26 06:06:07
回答 1查看 147关注 0票数 0

我正在尝试创建这个方法。让我们把这个叫做

代码语言:javascript
复制
-(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个最接近的色调,最后将它们包装到一个数组中,并返回以下内容。

做这件事最有效的方法是什么?提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-07-26 22:40:58

如果有人感兴趣,这里是我的方法。请随时对此发表评论。

代码语言:javascript
复制
-(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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6822924

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档