ios - 在 NSDictionary 中插入 nil 时不会抛出异常

标签 ios objective-c facebook nsdictionary null

所以我正在努力从用户的 Facebook 相册中提取照片,由于某种原因,NSDictionary 在插入 nil 时不会抛出异常。 我已经在真实设备和 iOS 模拟器(均为 iOS 7)上对其进行了测试。 也许调试器崩溃了,也许是 Facebook SDK 的错误。 无论如何,我愿意听取您的意见。

更新:还有一件事我忘了提。在调试器中,在字典中插入 nil 对象后,调试器本身继续执行程序。而且我什至没有按下按钮“继续执行程序”。

这是片段

- (void)downloadAlbumsWithCompletionBlock:(void (^)(NSArray *albums, NSError *error))completionBlock;
{
    [FBRequestConnection startWithGraphPath:@"/me/albums"
                                 parameters:nil
                                 HTTPMethod:@"GET"
                          completionHandler:^(FBRequestConnection *connection, id result, NSError *error)
    {
        if (error)
        {
            NSLog(@"%@", error.localizedDescription);
            completionBlock(nil, error);
        }
        else
        {
            NSMutableArray *data = [result objectForKey:@"data"];
            NSMutableArray *albums = [NSMutableArray new];
            for (NSMutableDictionary *dict in data)
            {
                NSString *albumID = dict[@"id"];
                NSString *coverPhotoID = dict[@"cover_photo"];
                // coverPhotoID gets nil and then successfully gets put in NSDictionary
                // You can just write "NSString *coverPhotoID = nil;".
                NSString *description = dict[@"description"];
                NSString *name = dict[@"name"];
                NSDictionary *album = @{@"album_id": albumID,
                                      @"album_cover_photo_id": coverPhotoID,
                                      @"album_description": description,
                                      @"album_name": name };
                // Here an exception should be thrown but for some reason
                // we just quit out of the method. Result would be the same if we put
                // empty "return;" statement.
                // If we explicitly put nil, like 
                //   @"album_cover_photo_id": nil
                // we get compiler error.
                [albums addObject:album];
            }
            completionBlock(albums, nil);
        }
    }];
}

最佳答案

你写道:

// Here an exception should be thrown but for some reason
// we just quit out of the method. Result would be the same if we put empty "return;" statement.

很可能在某处捕获了异常。尝试在 Objective-C 异常抛出上设置断点(在 Xcode 中,转到“断点”选项卡,单击左下角的 + 并选择“添加异常断点”。确保参数设置为“Objective-C 异常”和“抛出时中断”)。

关于ios - 在 NSDictionary 中插入 nil 时不会抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23808530/

相关文章:

objective-c - 通过 SSH 从 iOS 远程重新启动/关闭 mac?

ios - 如何绘制一个像 UICollisionBehavior 一样的圆形 UIView?

objective-c - 无法从 Cocoa NSMutableString 中去除换行符

Facebook Graph API 1.0 弃用

ios - 如何旋转到只有一个 View Controller 的横向

objective-c - 消失的 NSMutableArray,导致 UiTableView 显示空白单元格

iphone - 管理当前位置的缺失

iOS:UILabel 倒计时卡在其他 UI 操作上

facebook - 使用 Facebook oauth 登录,没有完整的 facebook.com 访问权限

ios - UIActivityViewController 在 iOS 7 上不显示 FB 和 Twitter