尝试按降序对部分进行排序

时间:2012-11-14 22:19:34

标签: objective-c xcode

        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 ___强发送到NSString类型的参数”

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

而崩溃

我需要更改什么才能使其工作并按降序对部分进行排序?感谢。

2 个答案:

答案 0 :(得分:2)

我不知道sortDescriptorWithKey的默认选择器:升序:现在是caseInsensitiveCompare:,我很确定它曾经只是比较:在任何情况下,您都可以使用sortDescriptorWithKey:ascending:selector:来代替,并为选择器传递compare :.我认为应该修复你的第二个错误。仍然不确定为什么你会收到第一个错误。

答案 1 :(得分:1)

如果您将代码格式化为易读性,那么您会做得更好(我们会更好地理解您)。例如:

    // 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];
    }

除此之外,这使得调试变得更加简单,因为您可以停止并转储section数组。

或者,如果你真的更喜欢它,那么我强烈建议你学习APL或LISP。