0

我已通过以下方式将内存分配给 NSMutable 数组

NSMutableArray *typedBookCollection=[[NSMutableArray alloc] init];

现在我将数组保存到可变字典中。

[self.bookCollectionDictionary setObject:typedBookCollection forKey:bookType];

字典的输出是

book details = {
    "Cookery Books" =     (
    );
    Others =     (
    );
    "Social Books" =     (
    );
    "Story Books" =     (
    );
    "Technical Books" =     (
    );
    "Travel Books" =     (
    );
}

我通过以下代码在指定的书籍数组中添加书籍

for(Book *localBook in books)
        {
            printf("\n\nBook found in database=%i",[books count]);
            printf("\nBook type found =%s",[localBook.type UTF8String]);
            NSMutableArray *typedBookArray=[self.bookCollectionDictionary objectForKey:localBook.type];
            NSLog(@"\nGoing to add books in typed array=%@",typedBookArray);


            [typedBookArray addObject:localBook];
            //[[self.bookCollectionDictionary objectForKey:localBook.type]addObject:localBook];
            printf("\nOne book added successfully in typedArray=%s",[localBook.title UTF8String]);
            NSLog(@"\nBook details=%@",localBook.title);

        }

但是每当我尝试通过 NSLog 打印它时它是(空)

4

2 回答 2

0

线

printf("\nBook type found =%s",[localBook.type UTF8String]);

产生(根据您的评论)输出

找到的书籍类型 = 旅行书籍

这意味着那是具有领先空间localBook.type的“旅行书” 。因此以下

NSMutableArray *typedBookArray=[self.bookCollectionDictionary objectForKey:localBook.type];

返回NULL:字典没有键“Travel Books”的值,仅适用于“Travel Books”。

于 2013-09-09T06:59:04.457 回答
0

因为您有数组字典(bookType)并且您必须将其添加到其他字典(书籍详细信息)中,而且您的内部字典(bookType)有空数组,所以每当您尝试通过NSLog它打印它时是(null)

于 2013-09-09T05:18:11.817 回答