【问题标题】:Downloading images with parse causes unrecognized selector error使用解析下载图像会导致无法识别的选择器错误
【发布时间】:2013-08-21 15:09:33
【问题描述】:

我正在尝试从 Parse 下载图像,当我从 parse 下载完所有图像后,我的程序崩溃并出现错误:

NSNull getDataInBackgroundWithBlock:progressBlock:]:无法识别的选择器发送到实例。

我已经尝试添加一些条件,例如

if(![object objectForKey:@"goal_image"]) //If it is nil => exit

但它仍然崩溃。这是我的代码:

        PFQuery *query = [PFQuery queryWithClassName:@"Goal"];

        [query getObjectInBackgroundWithId:[object objectId] block:^(PFObject *object, NSError *error) {

            if(!error)
            {
                PFFile * imageFile = [object objectForKey:@"goal_image"];
                if (imageFile) {

                    [imageFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error2) {
                        if (!error2) {

                            NSString  *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Images/Goals/%@",[object objectId]]];
                            [data writeToFile:jpgPath atomically:YES];

                        }
                    } progressBlock:^(int percentDone) {
                        if (percentDone == 100) {
                            NSLog(@"Download Completed");
                        }

                    }];
                }
            }
        }];

【问题讨论】:

    标签: ios objective-c ios6 parse-platform pfquery


    【解决方案1】:

    tl;博士

    除了检查nil之外,您还应该检查NSNull

    PFFile * imageFile = object[@"goal_image"]; // note the modern Obj-C syntax
    if (imageFile && ![image isEqual:[NSNull null]]) {
        ...
    }
    

    说明

    NSNullnil 不同,第一个是对象。

    由于NSArrayNSDictionary 仅保存对象,因此nil 不能存储在此类容器中,这就是为什么使用NSNull,通常表示JSON 响应中返回的null 值。

    nil 发送消息会静默失败,而向NSNull 单例发送无法识别的选择器将导致崩溃。

    还要记住objectForKey: 将返回nil,以防在字典中找不到密钥。

    【讨论】:

    • 非常感谢您的解释;)。我是 iOS 新手,不到一个月,还不知道这个区别。
    【解决方案2】:

    我知道这是旧的,但以防万一其他人遇到这个问题。对我来说,上面的答案没有用。对我有用的是在应用程序委托中启用本地数据存储。老实说,不知道为什么会这样,但我想我应该分享一下

    // Enable Local Datastore
    [Parse enableLocalDatastore];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-30
      • 2018-06-09
      • 1970-01-01
      相关资源
      最近更新 更多