2012-05-30 83 views
0

我有一個使用地址簿的應用程序。我試圖用從iPhone地址簿中排序聯繫人

sortedArray = [arr_contactList sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]; 

,然後當用戶選擇一個聯繫人,顯示從地址簿中的名稱排序列表,顯示其電話號碼。

我可以對iPhone地址簿電話號碼進行分類。

我用下面的電話號碼進行排序:

ABRecordRef source = ABAddressBookCopyDefaultSource(ab); 
NSArray *thePeople = (NSArray*)ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(ab, source, kABPersonSortByFirstName); 

NSString *name; 
for (id person in thePeople) 
{ 
    name = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty); 

    ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty); 

    for(CFIndex j = 0; j < ABMultiValueGetCount(phones); j++) 
    { 
     NSString* num = (NSString*)ABMultiValueCopyValueAtIndex(phones, j); 

     CFStringRef locLabel1 = ABMultiValueCopyLabelAtIndex(phones, j); 

     NSString *phoneLabel1 =(NSString*) ABAddressBookCopyLocalizedLabel(locLabel1); 

     [tempPhoneArray addObject:num];   
    }  
} 

但我實際的問題是,我的名字陣列具有特殊字符開始對列表的頂部接觸,當我選擇的手機號,聯繫人列表排序以字母A開始。所以我收到錯誤的電話號碼。

如何匹配兩種排序 - 名稱排序和數字排序?

回答

1

在這個例子中,你將製作27個數組,每個字母1個,但是這個概念可以應用於特殊字符與大寫/小寫的任何檢查。希望這可以幫助。

const int capacity = 27;  
NSMutableArray *subArrays = [NSMutableArray array]; 

//Prepopulate the subArray with 26 empty arrays 
for(int i = 0; i < capacity; i++) 
    [subArrays addObject:[NSMutableArray array]]; 
char currFirstLetter = 'a'; 

for(int i = 0; i < sortedArray.count; i++) 
{ 
    NSString *currString = [[sortedArray objectAtIndex:i] lowercaseString]; 

    NSLog(@"%@", currString); 
    NSLog(@"%c", [currString characterAtIndex:0]); 
    NSLog(@"%c", currFirstLetter); 

    if([currString characterAtIndex:0] == currFirstLetter) 
    { 
     //65 is the position of 'a' in the ascii table, so when we subtract 97, it correlates to 0 in our array. 
     [[subArrays objectAtIndex:currFirstLetter-97] addObject:[sortedArray objectAtIndex:i]]; 
    } 
    else if([currString characterAtIndex:0] < 65 || ([currString characterAtIndex:0] > 90 && [currString characterAtIndex:0] < 97) || [currString characterAtIndex:0] > 122) 
    { 
     //If it's a symbol (65-90 are uppercase, 97-122 are lowercase) 
     [[subArrays objectAtIndex:26] addObject:[sortedArray objectAtIndex:i]]; 
     //Increment the letter we're looking for, but decrement the count to try it again with the next letter 
    } 
    else 
    { 
     currFirstLetter++; 
     i--; 
    } 
} 
+0

什麼是子陣列?代碼崩潰在[subArrays objectAtIndex:26] :( – iOSDev

+1

我添加了代碼實例化子陣列,對此感到抱歉 – mergesort

+0

感謝您的代碼,但通過此代碼,數組排序列出聯繫人開頭的特殊字符頂部。特殊字符在末尾,與iPhone原生地址簿完全類似:(如何實現? – iOSDev

相關問題