gpt4 book ai didi

iphone - iPhone 中 NSMutableArray 的排序

转载 作者:行者123 更新时间:2023-12-03 16:53:22 27 4
gpt4 key购买 nike

我正在解析 XML 并将所有数据以字典的形式存储在 NSMutableArray 中。

NSMutableArray * stories;   

// a temporary item; added to the "stories" array one at a time,
// and cleared for the next one
NSMutableDictionary * item;

- (void)parser:(NSXMLParser *)parser
didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
{
if ([elementName isEqualToString:@"item"])
{
// save values to an item, then store that item into the array...
[item setObject:currentTitle forKey:@"title"];
[item setObject:currentLink forKey:@"link"];
[item setObject:currentSummary forKey:@"summary"];
[item setObject:currentDate forKey:@"date"];

//Numeric value for sorting
[item setObject:rates forKey:@"rates"];

[stories addObject:[item copy]];
}
}

完成解析后,我需要按数字字段的“费率”对数组进行排序。请提出建议。

最佳答案

假设 rates 键包含 NSNumbers:

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey: @"rates" ascending: YES];
[stories sortUsingDescriptors: [NSArray arrayWithObject: sortDescriptor]];
[sortDescriptor release];

在相关说明中,这行代码存在泄漏:

[stories addObject:[item copy]];

我认为你的意思是这样的:

[stories addObject:[[item copy] autorelease]];

关于iphone - iPhone 中 NSMutableArray 的排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2033048/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com