2012-07-30 107 views
1

我環顧四周,看到有關排序NSMutableArray的包含NSDictionaries,但一直沒有找到一些具體的例子,如何實際上使NSMutableArray組成NSDictionaries的一些具體的例子。創建一個NSMutableArray,其中包含幾個NSMutableDictionary對象

我正在使用NSUserDefaults跟蹤用戶對幾個不同項目的偏好。 NSMutableArray將用於跟蹤總項目,而NSDictionaries則用於每個項目的個人偏好。

我已經寫了這些方法來跟蹤我的偏好類中的項目,但我不太清楚如何初始化NMutableArray以包含NSDictionaries。

-(NSMutableArray *)deviceListArray 
{ 
    return [[NSUserDefaults standardUserDefaults] mutableArrayValueForKey:@"device_list_array"]; 
} 

-(void) setDeviceListArray:(NSMutableArray *)deviceListArray 
{ 
    [[NSUserDefaults standardUserDefaults] setObject:deviceListArray forKey:@"device_list_array"]; 
} 

如果任何人都可以指出我正確的方向,我會非常感激。非常感謝你!或者如果某人有更好的策略來跟蹤物品,每種物品都有不同的喜好,那麼這也會很棒!

謝謝!

回答

4

一旦你將一個對象傳遞給userDefaults,它擁有該對象 - 所以你不能去改變它裏面的字典的屬性。

你可以做的是一個雙向複製,其中你有一個可變字典的局部可變數組。您將創建一個新的可變數組,然後爲每個可變字典調用[NSDictionary dictionaryWithDictionary:],將新的不可變字典添加到新數組中。

如果您在字典中埋藏了可變對象,那麼您需要爲此做同樣的事情。所有這一切說,如果你有這麼多的東西,我懷疑你沒有正確地考慮如何使用默認系統。我幾乎總是隻保存字符串,數字,顏色等

編輯:代碼

NSMutableArray *array = [NSMutableArray arrayWithCapacity:10]; 

for(int i=0; i<10; ++i) { 
    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:10]; 
    // add stuff to the dictionary 
    [array addObject:dict]; 
} 

// later on want to change second dictionary: 
NSMutableDictionary *dict = [array objectAtIndex:1]; 
// fool around with dict 
// no need to re-save it in array, since its mutable 
+0

嘿感謝大衛。我其實沒有那麼多偏好。它與顯示無關,僅與外部設備進行交互,而且我只需要爲每個設備保存大約4件事情。你能否詳細說明在其中分配可變字典的可變數組的正確方法?這是真的把我扔了。我只是無法找到一個好例子。 – 2012-07-31 01:43:27

+0

我添加了一個例子。 – 2012-07-31 11:14:01

1

不知道這是否是你想要的功能:

#define kDeviceKey1 @"DeviceKey1" 
    #define kDeviceKey2 @"DeviceKey2" 
    #define kDeviceKey3 @"DeviceKey3" 
    #define kDeviceKey4 @"DeviceKey4" 
    #define kDeviceID @"DeviceID" 

    NSMutableArray *devices = [[NSMutableArray alloc]init]; 
    NSMutableDictionary *deviceInfo = [[NSMutableDictionary alloc]init]; 
    // create a dictionary record for earch device which contains thing... 
    // probably employ some kind of loop 
    deviceInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys: 
             [NSNumber numberWithInt:1], kDeviceID, 
//          @"DeviceName1", kDeviceID, // or this 
             @"whateverthing1forDevice1", kDeviceKey1, 
             @"whateverthing2forDevice1", kDeviceKey2, 
             @"whateverthing3forDevice1", kDeviceKey3, 
             @"whateverthing4forDevice1", kDeviceKey4, 
             nil]; 

    [devices addObject:deviceInfo]; 
    // this is just an example, you would have some kind of loop to replace "whateverthing1forDevice1" with actual data 
    deviceInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys: 
        [NSNumber numberWithInt:2], kDeviceID, 
//          @"DeviceName2", kDeviceID, // or this 
        @"whateverthing1forDevice2", kDeviceKey1, 
        @"whateverthing2forDevice2", kDeviceKey2, 
        @"whateverthing3forDevice2", kDeviceKey3, 
        @"whateverthing4forDevice2", kDeviceKey4, 
        nil]; 

    [devices addObject:deviceInfo]; 

    NSLog(@"devices: %@", devices); 
+0

嘿謝謝一堆例子。你是否也可以給我一些在設備內部抓取「whateverthing2forDevice2」的語法。我是否必須抓住字典,然後從那裏抓取它,還是可以一次完成? – 2012-08-01 00:44:21

+0

我現在將它作爲一個兩步,所以這不是什麼大問題。我只是想知道這是否可能 – 2012-08-01 01:11:04

相關問題