NSDictionary出错 - '对象计数与键数不同'

时间:2012-10-06 12:00:36

标签: objective-c cocoa nsdictionary crash-reports

在将我的应用程序提交到Mac App Store后,我刚收到崩溃报告(发布时),以供审核:

Application Specific Information:
objc[1832]: garbage collection is ON
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSDictionary initWithObjects:forKeys:]: count of objects (0) differs from count of keys (2)'
terminate called throwing an exception
abort() called

这是代码的一部分,我怀疑:

- (NSMutableDictionary*)getNewChapterWithTitle:(NSString*)title
{
    if (title==nil) title = [NSString stringWithFormat:@"Chapter %d",[[self appChapters] count]+1];

    // Align Justify
    NSMutableParagraphStyle * paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
    [paragraphStyle setAlignment:NSJustifiedTextAlignment];

    // Set Font
    NSDictionary *font = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:[NSFont fontWithName:@"Garamond" size:15.0],paragraphStyle, nil] forKeys:[NSArray arrayWithObjects:NSFontAttributeName, NSParagraphStyleAttributeName, nil]];

    NSString* lipsum = @"\nLorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc consequat fermentum faucibusPhasellus ac consequat metus. Sed at lectus sit amet tellus vulputate mollis vel suscipit metus. Nulla a sapien purus. Aenean sollicitudin lacus varius nisi faucibus nec lacinia mauris fermentum. Aliquam euismod quam rhoncus ipsum pulvinar quis condimentum magna imperdiet. Nullam augue leo, dictum tincidunt ullamcorper id, porta in lorem. Duis in vestibulum nisl.\
    \n\n\
    Donec imperdiet dignissim massa, at ultricies enim dignissim at. Etiam pharetra neque quis quam condimentum vitae viverra nisl volutpat. Vivamus urna ante, ultrices ut tincidunt id, auctor id est. Nunc eget diam purus, at viverra orci. Maecenas eu magna mi, id venenatis arcu. Maecenas ornare, tortor sit amet gravida placerat, diam urna lacinia nunc, a dictum arcu libero sed metus. Nulla pulvinar sapien vitae lectus faucibus quis tempus elit volutpat. Integer facilisis interdum lectus, sit amet dapibus enim mattis non. Donec sed pulvinar risus. Mauris in nulla urna, sit amet placerat turpis. Proin felis erat, pretium sed pharetra vel, tincidunt quis tortor. Donec ut nisi dui. Pellentesque gravida gravida justo, quis convallis leo euismod in. Nam pretium accumsan sapien ac interdum.";

    NSMutableAttributedString* theLipsum = [[NSMutableAttributedString alloc] initWithString:lipsum attributes:font];

    return [[NSMutableDictionary dictionaryWithObjectsAndKeys:
            [title retain],@"title",
            [theLipsum retain],@"content",
            [NSNumber numberWithInt:0],@"isMarkdown",
            nil] retain];
}

你觉得上面有什么“明显”错了吗? (它被称为4-5次,来自awakeFromNib,所以我猜这是罪魁祸首......)


提示:报告后添加了retain;我不知道它是否会有所帮助......

2 个答案:

答案 0 :(得分:4)

[NSFont fontWithName:@"Garamond" size:15.0]可能会返回nil。据我所知,它不是OSX上的默认字体。所以你的对象数组是空的,因为只有第一个nil之前的对象才被添加。

答案 1 :(得分:1)

我认为问题出在这里 - [NSFont fontWithName:@"Garamond" size:15.0] 此代码返回nil,因此数组为空。

可能此字体不是标准字体,但它已安装在您的计算机上。 如果问题在这里,您应该将此字体添加到应用资源。