2012-05-30 58 views
8

我已經和Person類分組對象在Objective-C

Person : NSObject{ 
    NSString *firstName; 
    NSString *lastName; 
    NSString *age; 
} 

NSMutableArray *personsArray = [NSMutableArray array]; 
Person *personObj1 = [[[Person alloc] init] autorelease]; 
personObj1.firstName = @"John"; 
personObj1.lastName = @"Smith"; 
personObj1.age = @"25"; 
[personsArray addObject: personObj1]; 

Person *personObj2 = [[[Person alloc] init] autorelease]; 
personObj2.firstName = @"John"; 
personObj2.lastName = @"Paul"; 
personObj2.age = @"26"; 
[personsArray addObject: personObj2]; 

Person *personObj3 = [[[Person alloc] init] autorelease]; 
personObj3.firstName = @"David"; 
personObj3.lastName = @"Antony"; 
personObj3.age = @"30"; 
[personsArray addObject: personObj3]; 

現在personsArray包含Person對象的3個對象的自定義對象的數組。

我們可以通過像age或firstName這樣的屬性對這個對象進行分組嗎?

我預期的結果是

NSDictionary { 
    "John" = >{ 
     personObj1, //(Its because personObj1 firstName is John) 
     personObj2 //(Its because personObj2 firstName is John) 
    }, 
    "David" = >{ 
     personObj3, //(Its because personObj3 firstName is David) 
    }, 
} 

我知道我可以通過創建一個NSDictionary得到這樣的結果,然後通過personsArray迭代,然後檢查每個第一

NSMutableDictionary *myDict = [NSMutableDictionary dictionary]; 

    for (Person *person in personsArray){ 

    if([myDict objectForKey: person.firstName]){ 
     NSMutableArray *array = [myDict objectForKey: person.firstName]; 
     [array addObject:person]; 
     [myDict setObject: array forKey:person.firstName]; 
    }else{ 
     NSMutableArray *array = [NSMutableArray arrayWithObject:person]; 
     [myDict setObject: array forKey:person.firstName]; 
    } 
    } 

    NSLog(@"myDict %@", myDict); 

//Result myDict will give the desired output. 

但有沒有更好的方法去做 ?

如果我使用的是@distinctUnionOfObjects,我只能按字符串對象進行分組(不是這樣的自定義對象,對吧?)。

感謝您提前回答。

+0

感謝它幫助我,我想要分組的同樣結果。:) –

回答

3

你可以試試,這是自定義對象分揀數組

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"Firstname" ascending:NO selector:@selector(compare:)]; 

[Array sortUsingDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]]; 
0

的代碼,以幫助你做這件事的下降,

-(NSMutableArray *)sortArray:(NSArray *)arrayTosort withKey:(NSString *)key ascending:(BOOL)_ascending 
{ 
    NSSortDescriptor *sortDescriptor; 
    sortDescriptor = [[NSSortDescriptor alloc] initWithKey:key 
               ascending:_ascending] ; 
    NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor]; 

    return [[arrayTosort sortedArrayUsingDescriptors:sortDescriptors] mutableCopy]; 
} 

這裏關鍵是keykeypath

0

NSArray中沒有按照方法的內置組。您的示例實現可以添加爲一個類別。

不幸的是,@distinctUnionOfObjects操作將只返回firstName屬性的字符串:John,David。它不會執行group by操作,也不會有這樣的操作。

Collection Operations

3

我創建了一個名爲Linq-to-ObjectiveC小庫,它提供了許多,這使得更容易查詢陣列的方法。爲了進行集團化運作,你需要,你可以簡單地做到以下幾點:

NSDictionary* groupedByName = [personsArray groupBy:^id(id person) { 
    return [person firstName]; 
}]; 

這將返回一個字典,其中鍵是不同的名字,每個值是有「人」對象的數組給定的名字。

1
- (NSDictionary *)groupObjectsInArray:(NSArray *)array byKey:(id <NSCopying> (^)(id item))keyForItemBlock 
{ 
    NSMutableDictionary *groupedItems = [NSMutableDictionary new]; 
    for (id item in array) 
    { 
     id <NSCopying> key = keyForItemBlock(item); 
     NSParameterAssert(key); 

     NSMutableArray *arrayForKey = groupedItems[key]; 
     if (arrayForKey == nil) 
     { 
      arrayForKey = [NSMutableArray new]; 
      groupedItems[key] = arrayForKey; 
     } 
     [arrayForKey addObject:item]; 
    } 
    return groupedItems; 
} 
11

結帳這個答案是非常相似的,你想

https://stackoverflow.com/a/15375756/1190861

什麼在你的情況下,代碼將如下所示:

NSMutableDictionary *result = [NSMutableDictionary new]; 

NSArray *distinctNames; 
distinctNames = [personsArray valueForKeyPath:@"@distinctUnionOfObjects.firstName"]; 
for (NSString *name in distinctNames) { 
    NSPredicate predicate = [NSPredicate predicateWithFormat:@"firstName = %@", name]; 
    NSArray *persons = [personsArray filteredArrayUsingPredicate:predicate]; 
    [result setObject:persons forKey:name]; 
} 

NSLog(@"%@", result);