- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个带字典的 NSMutableArray,所有字典都包含一个 NSDate,它是 NSString 的形式,键为“Date”。我想按日期对 NSDictionaries 进行排序。因此,例如我有以下数组状态:
Dict
Date
20.06.1996 23:30
Dict
Date
04.10.2011 19:00
Dict
Date
20.06.1956 23:39
我想对它进行排序,使其看起来像这样:
Dict
Date
20.06.1956 23:39
Dict
Date
20.06.1996 23:30
Dict
Date
04.10.2011 19:00
我已经尝试过 NSSortDescriptor,但没有成功...
更新:
我已经设法对日期进行排序,但我遇到了这个问题:在字典中不仅有日期,还有其他对象,我的代码所做的只是在字典之间切换日期值,而不是切换周围的完整字典。有了这个,字典中的其他值被分配了一个错误的日期,这是非常糟糕的。有谁能够帮助我?这是我的代码:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"savedData.daf"];
NSMutableArray *d = [NSMutableArray arrayWithContentsOfFile:path];
for (int ii = 0; ii < [d count]; ii++) {
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
if (is24h) {
[dateFormatter setDateFormat:@"dd.MM.yyyy HH:mm"];
}
else {
[dateFormatter setDateFormat:@"dd.MM.yyyy hh:mm a"];
}
[dateFormatter setLocale:[NSLocale currentLocale]];
NSDate *dat = [dateFormatter dateFromString:[[d valueForKey:@"Date"] objectAtIndex:ii]];
NSMutableDictionary *newDict = [[NSMutableDictionary alloc] init];
NSDictionary *oldDict = (NSDictionary *)[d objectAtIndex:ii];
[newDict addEntriesFromDictionary:oldDict];
[newDict setObject:dat forKey:@"Date"];
[d replaceObjectAtIndex:ii withObject:newDict];
[newDict release];
}
NSSortDescriptor *sorter = [[NSSortDescriptor alloc] initWithKey:@"Date" ascending:YES];
NSArray *sorters = [[NSArray alloc] initWithObjects:sorter, nil];
[sorter release];
NSMutableArray *sorted = [NSMutableArray arrayWithArray:[d sortedArrayUsingDescriptors:sorters]];
[sorters release];
NSLog(@"%@",sorted);
for (int ii = 0; ii < [sorted count]; ii++) {
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
if (is24h) {
[dateFormatter setDateFormat:@"dd.MM.yyyy HH:mm"];
}
else {
[dateFormatter setDateFormat:@"dd.MM.yyyy hh:mm a"];
}
[dateFormatter setLocale:[NSLocale currentLocale]];
NSString *sr = [dateFormatter stringFromDate:[[sorted valueForKey:@"Date"] objectAtIndex:ii]];
NSMutableDictionary *newDict = [[NSMutableDictionary alloc] init];
NSDictionary *oldDict = (NSDictionary *)[d objectAtIndex:ii];
[newDict addEntriesFromDictionary:oldDict];
[newDict setObject:sr forKey:@"Date"];
[sorted replaceObjectAtIndex:ii withObject:newDict];
[newDict release];
}
NSLog(@"before: %@"
""
"after: %@",d,sorted);
[sorted writeToFile:path atomically:YES];
最佳答案
有很多方法可以做到这一点,一种是使用 NSDate 对象而不是 NSString(或根据 ISO 8601 格式化的 NSString,以便字典排序与所需的排序相匹配)。然后你可以这样做:
NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"Date"
ascending:YES];
[array sortUsingDescriptors:[NSArray arrayWithObject:descriptor]];
或者,如果您不能(或不想)更改数据,您始终可以使用 block 进行排序:
[array sortUsingComparator:^(id dict1, id dict2) {
NSDate *date1 = // create NSDate from dict1's Date;
NSDate *date2 = // create NSDate from dict2's Date;
return [date1 compare:date2];
}];
当然,这可能会比第一种方法慢,因为您通常最终会创建超过 n 个 NSDate 对象。
关于objective-c - 按 NSDate 对 NSMutableArray 中的 NSDictionaries 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7628717/
我在使用以下代码来创建NSDictionaries的NSDictionary时遇到麻烦。没有编译错误,但是在运行时,此代码失败。 NSDictionary *section0 = [NSDiction
我得到了 '[NSDictionary]!? is not convertible to [NSDictionary]?' error on following code. var jsonRes
我有以下代码: NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; NSMutableDictionary *g
目前我有以下代码可以从 xml 文件中获取数据。这到目前为止有效 - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)el
在 Objective C 中,我们将字典值的 NSArray 分配给 NSDictionary 变量并获取键的值,如下所示,NSDictionary *dictValue = [array obje
将对象从一个 NSDictionary 复制到另一个 NSDictionary 的最简洁方法是什么?我必须从字典中提取特定值并将它们存储在新字典中。目前我有一些简单的代码,像这样...... NSSt
希望有人能帮助我: 我正在使用 NSDictionary 来填充 UITableView。它的模型类似于 [key:userID => value:userName]。tableView 只填充了 u
我有一本字典,其中包含从解析 JSON 中提取的其他字典中的子项。结构如下所示: { children = (
我需要检查 nsdictionary 中的元素是否不等于 normal,然后将该元素复制到一个数组中作为我的 UITable 的数据源。 这是一个条目的例子: Trailer = {
我正在尝试将数据添加到嵌套在其他词典中的 NSMutableDictionary。 在添加数据的代码运行之前开始输出 DayData Dictionary { //DayData
我的应用程序有一个 NSDictionary,其中包含许多其他 NSDictionary。如果我打印出这本词典,它的内容如下: oxip = { created = "2014-02-
我有一个 NSDictionary 记录这个: address = "30 East 23rd Street"; address1 = "30 East 23rd Street";
好的,我有一个 .plist 文件, 其中我有一个 NSDictionary *StoredAddresses, 在 StoredAddresses 中,是另一把 NSDictionary 我知道我可
您好,我在将数据从一个 NSDictionary 复制到另一个我使用的 NSDictionary 时遇到问题 [dicForFoodproduct_fromWeb initWithDictionary
我有一个包含四个对象的NSDictionary。每个对象都是一个包含数千个对象的 NSDictionary。我已经通过记录顶级字典的描述来验证它包含它应该包含的内容。但是,当我运行下面的代码以枚举该顶
给定的图像具有数据结构 我能够从 FirstText 和 LastText 获取数据,NSPredicate 是 subData.FirstText contains[cd] %@ OR subDat
我有一些装满书籍的 [NSDictionary],我正在检查这些书籍的值以便更好地在 UICollectionView 中显示内容。我正在检查一个 key 是否包含多个 ISBN 编号。如果是这样,我
我有一个很大的 NSDictionary,里面有一个较小的 NSDictionary。我想自动释放较大的一个,并保留第二个。我的初始化方法中有这段代码: // Autoreleased stage d
我对 Objective C 比较陌生。我有一个包含 NSDictionary 对象的 NSArray。我正在调用网络服务来获取数据。我有一个 PickerView,这是它的数据源(我实现了 numb
我想添加一个不同的字典作为另一个键字典的值,但在我的循环中,我发现相同的值添加到我所有的字典键。这是我的代码: for (MyObject *message in messages) {
我是一名优秀的程序员,十分优秀!