【问题标题】:Parse error iOS [unrecognized selector sent to instance]解析错误 iOS [无法识别的选择器发送到实例]
【发布时间】:2015-11-03 03:02:39
【问题描述】:

我有一个应用程序保存要解析的图像,它由第一个 VC 中的集合视图和一个详细视图控制器组成,您可以在其中通过 UIImagePickerController 添加图片。我之前在 Objective-C 中使用过 parse,但一直收到此错误

[PFObject setPhoto:]: unrecognized selector sent to instance 0x7fa15ac8f1c0

我在项目中有一个带有属性的解析类,我知道有不同的方法,但我通常这样做并且没有任何问题。

解析类头文件

#import <Parse/Parse.h>

@interface Picture : PFObject <PFSubclassing>

@property (nonatomic, strong) NSString *Caption;
@property (nonatomic, strong) NSArray *Likes;
@property (nonatomic, strong) PFFile *Photo;

+ (NSString *)parseClassName;

@end

.m 文件

#import "Picture.h"

static NSString * const ClassName = @"Picture";

@implementation Picture

@dynamic Caption;
@dynamic Likes;
@dynamic Photo;

+ (NSString *)parseClassName {

return ClassName;

}

@end

这是我保存图片的地方

- (IBAction)saveImage:(id)sender {

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Add caption" message:nil preferredStyle:UIAlertControllerStyleAlert];

[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {

    textField.placeholder = @"title";

}];

[alertController addAction:[UIAlertAction actionWithTitle:@"Save" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

    NSArray *textFields = [alertController textFields];

    NSString *nameString = ((UITextField *)textFields[0]).text;
    NSData *imageData = UIImagePNGRepresentation(self.chosenImage);

//        PFObject *object = [PFObject objectWithClassName:@"Picture"];

    Picture *picture = [Picture object];

    PFFile *file = [PFFile fileWithData:imageData];


    [picture saveInBackgroundWithBlock:^(BOOL succeeded, NSError * _Nullable error) {

        if (succeeded) {

            picture.Photo = file;
            picture.Caption = nameString;

            [picture saveInBackground];
            [picture pinInBackground]; 

            UIAlertController *successAlert = [UIAlertController alertControllerWithTitle:@"Photo Saved!" message:nil preferredStyle:UIAlertControllerStyleAlert];

            [successAlert addAction:[UIAlertAction actionWithTitle:@"Okay" style:UIAlertActionStyleCancel handler:nil]];

            [self presentViewController:successAlert animated:YES completion:nil];
        }

        else {

            NSLog(@"%@", error.localizedDescription);
        }

    }];

    [alertController addAction:[UIAlertAction actionWithTitle:@"Okay!" style:UIAlertActionStyleCancel handler:nil]];

    //save to parse here

}]];

[self presentViewController:alertController animated:YES completion:nil];

}

//image picker delegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {

self.chosenImage = info[UIImagePickerControllerEditedImage];
self.imageView.image = self.chosenImage;

[picker dismissViewControllerAnimated:YES completion:nil];

}

起初我以为这可能是图像的大小,但是当我注释掉该部分并尝试保存标题时,我得到了同样的错误,但使用的是“setCaption”而不是“setPhoto”

更新:对象已保存以进行解析,但列中没有数据,键(属性)拼写正确,所有内容也是如此。

谢谢!

【问题讨论】:

    标签: objective-c parse-platform ios9


    【解决方案1】:

    问题似乎是动态生成的设置器没有在您的自定义 PFObject 上创建。

    问题可能是您选择的属性案例。 Apple 定义的约定是以小写字母开头的属性,然后是驼峰式。您应该将大小写更改为小写,因为这可能会导致与 getter 的创建发生冲突。

    你已经有了

    PFObject *object = [PFObject objectWithClassName:@"Picture"];
    

    我猜你的代码不能用它。

    我可以建议的另一件事是移动

    picture.Photo = file;
    picture.Caption = nameString;
    

    出块,放在PFFile *file = [PFFile...之后并删除

    [picture saveInBackground];
    

    因为您似乎要保存图片对象两次。根据与您在 Parse 中获得的空记录相对应的描述。因此,该保存正在成功执行。这是第二次保存失败。

    另外,尝试添加

    #import <Parse/PFObject+Subclass.h>
    

    到您的实现 (.m) 文件,因为这是定义 object 的类方法的位置。

    【讨论】:

    • 那行不通。不过,这真的很奇怪,我有另一个项目可以正常工作。你认为这是 iOS 9 的事情吗?
    • 我不确定。我怀疑它与您的 Parse 安装有关,例如它以某种方式损坏。你是用 CocoaPods 安装的吗?还是其他方法?
    • 不,我只是从解析网站的快速启动部分拖入框架,然后手动添加另一个。
    • 为了隔离问题,我建议创建一个新项目,重新安装必要的库。然后在新项目中测试你的代码。
    • 是的,我可以这样做,我只是很好奇这个有什么问题。
    猜你喜欢
    • 1970-01-01
    • 2011-12-24
    • 2012-03-14
    • 2014-02-18
    • 1970-01-01
    • 2012-04-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多