SBJson返回nil导致可可窗口不显示

时间:2010-05-09 18:35:24

标签: cocoa json

我正在使用示例here。如果密钥没有值,我的应用程序将在控制台中返回-[NSCFArray insertObject:atIndex:]: attempt to insert nil,并且我的窗口不会显示。我怎么能预先检查以确保不会发生这种情况?

1 个答案:

答案 0 :(得分:1)

测试对象指针是否为nil

if (theObject != nil) {
    [myArray insertObject:theObject atIndex:idx];
} else {
    //The pointer is nil. This probably means the JSON framework gave you an error object (assuming you enabled it to), in which case, now's probably the time to present it.
    if (error != nil)
        [NSApp presentError:error];
}