附加关键NSMutableDirectory的值

时间:2017-01-21 20:42:39

标签: objective-c dictionary nsdictionary nsmutabledictionary

我正在处理objective c代码,我有一个我正在循环的数组,并根据结果向键添加值。

然而,当需要时,我想附加特定键的值,但我完全不知道如何用目标c实现这一点。

我之前已经在swift中为此写了一个解决方案,但我似乎无法弄清楚如何在目标c中应用相同的东西。这是我目前拥有的

-(void)makeDirectory:(NSArray*)aList {
NSMutableDictionary *dic = [NSMutableDictionary new];

for (NSDictionary *entry in aList) {
    NSString *character = @"";
    xbmcMovie *movie = [[xbmcMovie alloc] initWithDictionary:entry];
    NSString *name = movie.Title.stripSpecialCharacters.stripArticle.stripSpaces.stripNumber;
    character = name.character;

    if (![dic objectForKey:character]) {
        [dic setObject:movie forKey:character];
    }
    ///[dic ]
}

}

这是我在swift 3.0中所做的

func makeDictionary(_ libraryArray: [songsClass])->[String: [songsClass]]{
    var dic = [String: [songsClass]]()

    for entry in libraryArray {
        var character: String = ""
        var songName = entry.songName.stripSpecialCharacters().stripArticle().stripSpaces().stripNumber()

        guard let char = songName.characters.first?.string() else { return [:] }
        character = char

        if dic[character] == nil {
            dic[character] = [songsClass]()
        }

        dic[character]!.append(entry)

    }
    return dic
}

1 个答案:

答案 0 :(得分:2)

因此,如果我已经正确理解了这一点,那么你在字典中的值是一个包含电影的数组,并且你想要添加到这个数组中,所以你需要做的是:

NSMutableArray *movieArray = [NSMutableArray new];
[movieArray addObject:movie];
if (![dic objectForKey:character]) {        
   [dic setObject:movieArray forKey:character];
}

//If you want to add to this array again then you can do

[movieArray addObject:anotherMovie];

//Or if you need to retrieve the array again
movieArray = [dic objectForKey:character];
[movieArray addObject:anotherMovie];

您的代码也有

character = name.character;

其中name是NSString?