0
        self.sections = [[NSMutableDictionary alloc] init];
        BOOL found;
        for (NSDictionary *wine in sortedWines)
        {
            NSNumber *rate = [wine valueForKey:@"Rate"];
            NSString *rateStr = [NSString stringWithFormat:@"%.f", [rate floatValue]];
            found = NO;
            for (NSString *str in [self.sections allKeys])
            {
                if ([str isEqualToString:rateStr])
                {
                    found = YES;
                }
            }
            if (!found)
            {[self.sections setValue:[[NSMutableArray alloc] init] forKey:rateStr];}
        }
        for (NSDictionary *wine in sortedWines)
        {[[self.sections objectForKey:[NSString stringWithFormat:@"%.f", [[wine valueForKey:@"Rate"] floatValue]] ] addObject:wine];}

        // Sort:
        for (NSString *key in [self.sections allKeys])
        {[[self.sections objectForKey:key] sortUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"Rate" ascending:NO]]];}

这段代码将我的葡萄酒分成几个部分,但它不会按降序对它们进行排序!难道是因为 NSNumber 被转换成了 NSString?我尝试使用 NSNumber 值编写代码:

        self.sections = [[NSMutableDictionary alloc] init];
        BOOL found;
        for (NSDictionary *wine in sortedWines)
        {
            NSNumber *rate = [wine valueForKey:@"Rate"];
            found = NO;
            for (NSNumber *str in [self.sections allKeys])
            {
                if ([str isEqualToNumber:rate])
                {
                    found = YES;
                }
            }
            if (!found)
            {[self.sections setValue:[[NSMutableArray alloc] init] forKey:rate];}
        }
        // Loop again to sort wines into their keys
        for (NSDictionary *wine in sortedWines)
        {[[self.sections objectForKey:[wine valueForKey:@"Rate"]] addObject:wine];}

        // Sort each section array
        for (NSString *key in [self.sections allKeys])
        {[[self.sections objectForKey:key] sortUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"Rate" ascending:NO]]];}

但它给出了一个警告

if (!found)
{[self.sections setValue:[[NSMutableArray alloc] init] forKey:rate];} 

上面写着“不兼容的指针类型将 NSNumber ___strong 发送到 NSString 类型的参数”

如果我运行该应用程序,它会因错误而崩溃-[__NSCFNumber localizedCaseInsensitiveCompare:]: unrecognized selector sent to instance 0x1e085810

我必须进行哪些更改才能使其正常工作并按降序对部分进行排序?谢谢。

4

2 回答 2

2

我不知道 sortDescriptorWithKey:ascending: 的默认选择器现在是否为 caseInsensitiveCompare:,我很确定它曾经只是 compare:。在任何情况下,您都可以使用 sortDescriptorWithKey:ascending:selector: 代替,并为选择器传递 compare:。我认为这应该可以解决您的第二个错误。仍然不确定为什么会出现第一个错误。

于 2012-11-15T01:37:01.630 回答
1

You would do much better (and we'd understand you better) if you formatted your code for legibility. Eg:

    // Loop again to sort wines into their keys
    for (NSDictionary *wine in sortedWines) {
        NSArray* section = [self.sections objectForKey:[wine valueForKey:@"Rate"]];
        [section addObject:wine];
    }

    // Sort each section array
    NSArray* sortDescriptorArray = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"Rate" ascending:NO]];
    for (NSString *key in [self.sections allKeys]) {
         NSArray* section = [self.sections objectForKey:key];
         [section sortUsingDescriptors:sortDescriptorArray];
    }

Among other things, this makes debugging much simpler since you can stop and dump the section arrays.

Or, if you really liked it better the other way, I can highly recommend that you learn APL or LISP instead.

于 2012-11-15T02:12:44.363 回答