我有這樣的代碼片斷,導致整個數組中包含一大堆「< null>」,我需要弄清楚如何刪除它們。很明顯,在將我的頭撞到鍵盤後,我正在尋求一些幫助。從對象中刪除空數組-c
在我的.h我宣佈:
NSArray *sortedContacts;
NSArray *rawContacts;
然後在.M:
-(void) buildContacts {
ABAddressBook *addressBook = [ABAddressBook sharedAddressBook];
NSArray *contacts = [addressBook people];
rawContacts=contacts;
NSArray *firstNames = [rawContacts valueForKey:@"First"];
NSArray *lastNames = [rawContacts valueForKey:@"Last"];
NSArray *organization = [rawContacts valueForKey:@"Organization"];
NSMutableArray *fullNames = [NSMutableArray array];
for(int i = 0; i < [firstNames count]; i++)
{
NSString *fullName = [NSString stringWithFormat:@"%@ %@ %@",
[firstNames objectAtIndex:i],
[lastNames objectAtIndex:i],
[organization objectAtIndex:i]];
[fullNames addObject:fullName];
}
NSMutableArray *fullList = [[NSMutableArray alloc]initWithArray:fullNames];
[fullList removeObjectIdenticalTo: @"<null>"];
sortedContacts = [fullList sortedArrayUsingSelector:@selector(compare:)];
NSLog(@"%@",sortedContacts);
}
我試過這麼多東西,我只是不能看到的森林樹木了。
可能重複http://stackoverflow.com/q/110332/2557145 – Palle