为键 NSMutableDirectory 附加值

append values for key NSMutableDirectory

我目前正在编写 objective c 代码,其中我有一个循环遍历的数组,并根据结果向键添加一个值。

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

我之前在 swift 中为此写过一个解决方案,但我似乎无法弄清楚如何在 objective 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
}

所以如果我没有理解错的话,你在字典中的值是一个包含电影的数组,你想添加到这个数组中,所以你需要做的是:

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;

其中名称是 NSString?