我有一個數組unsortedPosts
滿字典,它看起來是這樣的,如果我在終端內容打印出來NSDictionaries的NSArray的:排序使用NSSortDescriptor不工作
{
"created_at" = 1332584959;
id = "61926802418418";
network = "net1";
text = "text1";
url = "https://www.google.com";
},
{
"created_at" = 1332581096;
id = "6192689218418";
network = "net1";
text = "text1";
url = "https://www.yahoo.com";
},
{
"created_at" = 1332581074;
id = "6192688628418";
network = "net1";
text = "text1";
url = "https://www.aol.com";
}
我想通過created_at
場對它們進行排序(unix時間戳)。我正在使用NSSortDescriptor,如下所示:
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"\"created_at\"" ascending:TRUE];
NSMutableArray *sortDescriptors = [NSMutableArray arrayWithObject:sortDescriptor];
NSArray *sortedSocialPosts;
sortedSocialPosts = [unsortedSocialPosts sortedArrayUsingDescriptors:sortDescriptors];
但輸出未排序。
編輯
我試圖運行它,而無需使用周圍created_at
轉義引號(每@Eimantas答案):
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"created_at" ascending:TRUE];
NSMutableArray *sortDescriptors = [NSMutableArray arrayWithObject:sortDescriptor];
NSArray *sortedSocialPosts;
sortedSocialPosts = [unsortedSocialPosts sortedArrayUsingDescriptors:sortDescriptors];
但我得到這個錯誤:
-[__NSCFNumber length]: unrecognized selector sent to instance 0x6a2a3c0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber length]: unrecognized selector sent to instance 0x6a2a3c0'
如果陣列印有'的NSLog()''周圍的created_at'報價只是呈現方式的神器吧打印的東西 - 很可能是因爲下劃線的 – JeremyP 2012-03-26 08:18:00