想法?从NSDictionary键值创建排序数组(带对象)

时间:2013-08-06 22:53:07

标签: sorting nsdictionary selector

我知道有很多问题已经在stackoverflow中被问及并回答了在NSDictionary中获取密钥的挑战,将这些密钥放入数组中的排序顺序。我知道对象不是按照实际字典中的排序顺序存储的,我认为,出于效率原因或者可能是基础代码的内存管理。

我一直在努力尝试从这里以及苹果文档和博客中的几个答案中的示例(有些我可以开始工作,有些则不是!),但我似乎无法找到一个解决我的问题的例子混乱。

我认为我的困惑是我在这里遇到的例子,在苹果文档和不同的有用博客中,似乎都有一些例子,其中只有一个键值对,第二个值不是对象 - 它更像是一个值。 (但是,某种程度上它不是真正的对象吗?我认为是这样)

我无法开始工作的一个例子(Sorting an NSArray by an NSDictionary value)使用了这个想法

   [array sortedArrayUsingComparator:^(NSDictionary *item1, NSDictionary *item2) {
    NSString *age1 = [item1 objectForKey:@"age"];
    NSString *age2 = [item2 objectForKey:@"age"];
    return [age1 compare:age2 options:NSNumericSearch];
    }];

我想也许这个想法,以更具体的方式指定密钥,可能是我的问题。

我想知道我是否可能没有与编译器通信密钥是什么,对象是什么,这就是为什么我将#34;无法识别的选择器发送到实例&#34 ;错误。

..... Code Snips关注.....

1) 我有一个名为" Dog"的课程。给定的dog对象有几个属性,包括NSString键。

我的关键是" licenseString"是一个字母数字键 - 我也想知道我是否应该使用decimalNumberWithString,但这不是问题

 @property (strong,nonatomic) NSString *licenseString;
  @property (strong, nonatomic) NSString *dogName;
 @property (strong, nonatomic) NSString *whatMakesDogSpecial;
 @property (strong, nonatomic) UIImage *dogPhoto;

2)我有一个NSDictionary   @property(nonatomic,strong)NSDictionary * dogDictionary;

我用这种不太复杂的方式将信息硬编码到dogDictionary中,

 Dog *aDog;

// Dog one
aDog = [[Dog alloc]init] ;

aDog.licenseString     = @"1";
aDog.dogName   = @"Oscar";

aDog.whatMakesDogSpecial = @"This animal was found at the Mid-Penn humane society. He is super friendly, plays well with other dogs and likes people too. He enjoys fetching balls and frisbees too. He also goes to the park daily."   ;

aDog.dogPhoto   = [UIImage imageNamed:@"webVuMiniAppDogOscar.jpg"];

[self.dogDictionary setValue:aDog forKey:aDog.licenseString];

3)然后,一旦我的dogDictionary中有多个狗对象,我想对许可证标记值进行排序,这样我就可以使用狗名称填充表格视图,但是按照许可证标签的顺序填充。

似乎编译器确实认识到" vars.dogDictionary"它出现在下面的代码片段中,因为当我查看调试器时,我可以看到两个有效的实例来自我的狗词典。调试器输出位于附件

所以,使用stackoverflow答案和apple文档中的想法,我写这个

 NSArray *sortedKeys = [vars.dogDictionary keysSortedByValueUsingComparator:
                       ^NSComparisonResult(id obj1, id obj2) {
                           return [obj1 compare:obj2];
                       }];


NSLog(@" The sorted array is %@", sortedKeys);

那就是我的问题发生的地方。我认识到0x1182f740指的是" obj1"如调试器附件中所示

2013-08-06 15:13:58.276 SortDogLIcenseTags [3876:11303] - [狗比较:]:无法识别的选择器发送到实例0x1182f740 (lldb)

附件是显示调试器值的图片 - 他们不想粘贴得很好

From the debugger - valid dog objects are showing up as obj1 and obj2 with properties I put in a

1 个答案:

答案 0 :(得分:0)

以下是我如何解决这一挑战。它的工作原理非常简单,可以很好地集成到我的Master / Detail项目中

我知道我在网上发现了一个教程,引导我找到这个解决方案,对不起,我现在找不到它。

请注意,sortedDogDictionaryArray和dogDictionaryArray在.h文件中声明为属性。

self.dogDictionaryArray = [vars.dogDictionary allValues];
// Sort

NSSortDescriptor *sortDescriptorDog =
[[NSSortDescriptor alloc] initWithKey:@"licenseString" ascending:YES];
NSArray *sortDescriptorsDogs =
[NSArray arrayWithObject:sortDescriptorDog];


self.sortedDogDictionaryArray =
[self.dogDictionaryArray sortedArrayUsingDescriptors:sortDescriptorsDogs];
NSLog(@"%@",self.sortedDogDictionaryArray );

int doggie;
Dog *someDogName;
NSLog(@"Sorted Order is...");

for (doggie = 0; doggie < [self.sortedDogDictionaryArray count]; doggie++) {


    //NSLog(@"%@", [sortedArray objectAtIndex:i]);
    //NSLog(@"%@", [sortedArrayDogs objectAtIndex:doggie]);
    someDogName = [self.sortedDogDictionaryArray   objectAtIndex:doggie];
    //NSLog(@"name is %@", someDogName.dogName);
    NSLog(@"name is %@ tag is %@", someDogName.dogName, someDogName.licenseString);


}