【问题标题】:Add multiple objects under key in NSMutableDictionary在 NSMutableDictionary 的 key 下添加多个对象
【发布时间】:2014-09-07 23:41:02
【问题描述】:

我想要实现的是以下,将一个值(关键字)存储在一个字典中的键(帐户)下,然后将该字典存储在另一个带有键(列表)的字典下,最后将该字典存储在另一个字典下键(allKeys)。

- (void)addFilterForAccount:(NSString *)account forKeyword:(NSString *)keyword inList:(NSString *)list {

    //Set properties for NSStrings
    account = _accountName;
    keyword = _textField2.text;
    list = _listName;
    //Clear the textField
    _textField2.text = @"";
    //Store this information into a dictionary/array
    _keys = [[NSMutableDictionary alloc] init];
    [_keys setObject:keyword forKey:account];

    _keywords = [[NSMutableDictionary alloc] init];
    [_keywords setObject:_keys forKey:list];

    _filters = [[NSMutableDictionary alloc] init];
    [_filters setValue:_keywords forKey:@"allKeys"];

    //nomenclature would be:
    //
    //_filters[@"allKeys"][@"//listName\\"][@"//accountName\\"]

    NSLog(@"%@", _filters);
}

我有以下代码应该做我想做的事,但此时的问题是它不允许多个键。所以如果我想在一个列表下存储多个关键字,当我注销时应该是这样的:

2014-09-07 18:31:12.562 Filterfeed[4050:124143] {
    allKeys =     {
        "Breaking News" =         (
                        {
                BreakingNews = romney
                               obama
                               bush
                               clinton;
            }
        );
    };
}

但现在它只是一个名称,每次我调用此方法时都会被替换。

实际打印出来的内容:

2014-09-07 18:31:12.562 Filterfeed[4050:124143] {
    allKeys =     {
        "Breaking News" =         (
                        {
                BreakingNews = romney;
            }
        );
    };
}

【问题讨论】:

  • 您有一个基本问题,即每次都为您分配新字典,而且您的“所需”所示数据结构有点混乱。您最里面的结构有多个与单个键关联的值 - 那么它应该是一个数组吗?
  • 是的,我认为应该是,但数组必须包含关键帐户字符串下的关键字。阵列可以做到这一点吗?然后将该数组存储到键列表字符串下的字典中。

标签: ios objective-c nsarray nsdictionary


【解决方案1】:

你希望这个方法是:

- (void)addFilterForAccount:(NSString *)account forKeyword:(NSString *)keyword inList:(NSString *)list { 

//Set properties for NSStrings 
account = _accountName; 
keyword = _textField2.text; 
list = _listName; 
//Clear the textField 
_textField2.text = @""; 
//Store this information into a dictionary/array 
_keys = [[NSMutableDictionary alloc] init]; 
NSMutableArray *array = [NSUserDefualts standardUserDefaults] objectForKey: list]; 
[array addObject: keyword]; 
[[NSUserDefualts standardUserDefaults] setObject: array forKey: list]; 
[[NSUserDefualts standardUserDefaults] synchronize]; 
NSMutableArray *array2 = [NSUserDefualts standardUserDefaults] objectForKey: list]; 
[_keys setObject:array2 forKey:account]; 
_keywords = [[NSMutableDictionary alloc] init]; 
[_keywords setObject:_keys forKey:list]; 

_filters = [[NSMutableDictionary alloc] init]; 
[_filters setValue:_keywords forKey:@"allKeys"]; 

//nomenclature would be: 
// 
//_filters[@"allKeys"][@"//listName\\"][@"//accountName\\"] 

NSLog(@"%@", _filters); 
}

【讨论】:

  • 问题是,这个数组必须由帐户组织。所以我需要将关键字添加到帐户字符串键下的数组中,然后将该数组添加为列表下的键?
  • 对不起,我弄错了。我的意思是说您将数组设置为键的对象。
  • 您希望数组成为您的关键字列表。
  • 对,但是数组应该将关键字存储在帐户字符串下。
  • 是的,帐户密钥。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多