2011-08-29 66 views
1

我的代碼:的Objective-C的NSMutableArray的setValue forKey問題

NSArray *array = //objects here 
NSMutableArray *mutarray = [[NSMutableArray alloc] init]; 

    for (NSString *s in array) 
    { 
     NSRange ran = [s rangeOfString:@"abc"]; 
    if (ran.location != NSNotFound) 
    { 
     int inde = [array indexOfObject:s]; 
     [mutarray setValue:[NSNumber numberWithInt:inde] forKey:s]; 
    } 
    } 
    NSLog (@"mutarray: %@", mutarray); 

我已經試過的NSMutableDictionary,但內容是無序的,所以我更喜歡的NSMutableArray存儲值和鍵有序。但是NSLog的輸出沒有顯示任何內容,到底是什麼? NSMutableDictionary的輸出顯示了四個發現。


編輯:

我甚至試圖在的NSLog面前這個代碼,但沒有顯示:

[mutarray setValue:@"aha" forKey:@"jaha"]; 

編輯: 夥計們,感謝您的回答!現在我明白了。但問題是我將數字作爲值和字符串存儲爲鍵。我有一個無序的NSMutableDictionary。我還沒有想出我應該如何訂購它。我有我使用的代碼,但它不正確寫入:

NSInteger intSort(id num1, id num2, void *context) 
{ 
    int v1 = [num1 intValue]; 
    int v2 = [num2 intValue]; 
    if (v1 < v2) 
     return NSOrderedAscending; 
    else if (v1 > v2) 
     return NSOrderedDescending; 
    else 
     return NSOrderedSame; 
} 

在其他方法:

NSArray *ordered = [[mutdic allValues] sortedArrayUsingFunction:intSort context:NULL]; 
NSLog (@" ordered: %@", ordered); 

輸出只顯示:

2011-08-29 22:36:03.998 scanner2[32359:207] ordered: (
    1, 
    5, 
    9, 
    16 
) 

的輸出應該顯示這個按數字順序排列:

2011-08-29 22:36:03.992 scanner2[32359:207] mutdic: 5 jam (geologi) en 
2011-08-29 22:36:03.993 scanner2[32359:207] mutdic: 9 jax. (geologi)jammen 
2011-08-29 22:36:03.994 scanner2[32359:207] mutdic: 1 beta (geologi). 
2011-08-29 22:36:03.995 scanner2[32359:207] mutdic: 16 .(geologi),be;ta; 

回答

6

NSMutableArray不存儲鍵/值對,它只存儲對象。要將對象添加到陣列,請使用-addObject:方法。

使用數組來存儲對象的有序列表。當您需要查找給定某個鍵的對象時,請使用字典。如果你想要一本字典的鍵的有序列表,使用

NSArray* orderedKeys = 
    [[myDictionary allKeys] sortedArrayUsingSelector:@selector(compare:)]; 

編輯:一對夫婦的更多選項。

  1. 只存儲索引數組,然後在字符串的主要array查找值。

    for (NSUInteger i = 0; i < [array count]; ++i) 
    { 
        NSString* s = [array objectAtIndex:i]; 
        NSRange ran = [s rangeOfString:@"abc"]; 
        if (ran.location != NSNotFound) 
        { 
         [mutarray addObject:[NSNumber numberWithInt:i]]; 
        } 
    } 
    
    for (NSNumber* index in mutarray) 
    { 
        NSLog(@"Found %@:%@", index, [array objectAtIndex:[index intValue]]); 
    }   
    
  2. 索引存儲和字符串的二維數組:

    for (NSUInteger i = 0; i < [array count]; ++i) 
    
    { 
        NSString* s = [array objectAtIndex:i]; 
        NSRange ran = [s rangeOfString:@"abc"]; 
        if (ran.location != NSNotFound) 
        { 
         [mutarray addObject: 
          [NSArray arrayWithObjects:[NSNumber numberWithInt:i], 
                 s, 
                 nil]]; 
        } 
    } 
    
+0

這是我需要的第二個選項!我愛你:D – wagashi

3

NSArray是對象的有序集合。它不是對象的鍵值映射。在NSArray上調用setValue:forKey:與向其每個項目發送setValue:forKey:相同。

1

這setValue方法:ForKey:方法是不是從NSMutableArray裏,它是NSKeyValueCodingProtocol

在你的情況,因爲你想訂購了一本字典的結果,你可以看看下面的方法:

- keysSortedByValueUsingSelector: - keysSortedByValueUsingComparator: - keysSortedByValueWithOptions:usingComparator:

如果需要排序的值,只需使用allKeys它返回一個數組。您可以將它們的排列使用以下方法進行排序:

- sortedArrayHint - sortedArrayUsingFunction:背景: - sortedArrayUsingFunction:背景:提示: - sortedArrayUsingDescriptors: - sortedArrayUsingSelector: - sortedArrayUsingComparator: - sortedArrayWithOptions:usingComparator :

1

它看起來像你想存儲一個有序的列表對。所以NSArray的NSArray

for (NSString *s in array) 
{ 
    NSRange ran = [s rangeOfString:@"abc"]; 
    if (ran.location != NSNotFound) 
    { 
     int inde = [array indexOfObject:s]; 
     NSArray* pair = [NSArray arrayWithObjects:[NSNumber numberWithInt:inde],s,nil]; 
     [mutarray addObject: pair]; 
    } 
} 
+0

謝謝。這就是Darren的第二種選擇,除非你製作了兩個NSArrays的NSMutableArray。 – wagashi