【问题标题】:Can't set property to NSManagedObject无法将属性设置为 NSManagedObject
【发布时间】:2013-06-18 15:05:56
【问题描述】:

我是使用 Core Data 的新手。 我有 Chat 类(NSManagedObject 的子类),我需要用它创建新对象。

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>

@interface Chat : NSManagedObject

@property (nonatomic, retain) NSString *chatID;
@property (nonatomic, retain) NSString *modified;
@property (nonatomic, retain) NSString *opponentID;
@property (nonatomic, retain) NSString *opponentFacebookID;
@property (nonatomic, retain) NSString *opponentName;
@property (nonatomic, retain) NSString *lastMessage;

@end

#import "Chat.h"

@implementation Chat

@dynamic chatID;
@dynamic modified;
@dynamic opponentID;
@dynamic opponentFacebookID;
@dynamic opponentName;
@dynamic lastMessage;

- (id)init {
    if (self = [super init])
    {
    }
    return self;
}

@end

我像这样插入对象:

Chat *chat = (Chat *)[NSEntityDescription insertNewObjectForEntityForName:@"Chat" inManagedObjectContext:[[DataManager sharedInstance] managedObjectContext]];
chat.chatID = [chatDict objectForKey:@"id"];
chat.modified = [chatDict objectForKey:@"modified"];
chat.lastMessage = [chatDict objectForKey:@"last_message"];

但我的应用程序在设置chat.chatId 时崩溃。它向我显示了这个错误,无法弄清楚如何修复它!

2013-06-18 12:01:25.625 MyApp[5477:907] -[Chat setChatID:]: unrecognized selector sent to instance 0x1d8661d0
2013-06-18 12:01:25.627 MyApp[5477:907] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Chat setChatID:]: unrecognized selector sent to instance 0x1d8661d0'

【问题讨论】:

  • 摆脱init 方法。 This answer 也会帮助你
  • @Alladinian:你是对的,这也可能导致问题。

标签: objective-c core-data


【解决方案1】:

这可能意味着chatID 不是 核心数据模型检查器。

我总是建议让 Xcode(或“mogenerator”之类的工具)生成托管对象子类文件 而不是“手动”编写它们。

【讨论】:

  • 就是这样!我的模型具有不同的类属性名称。我再次自动生成了课程!谢谢!
【解决方案2】:

无法识别的选择器意味着应用程序试图告诉一个对象执行它没有的功能(例如,尝试修改标签的图像,它没有图像属性,所以它会抛出一个无法识别的选择器)。在错误消息中,它尝试使用不存在的 setChatID。我在这台计算机上没有我的 xcode 项目,所以我无法检查我的核心数据样本,但我建议重新制作 Chat 对象并使用 GUI 创建您需要的属性,然后自动创建类。如果您希望我提供更多帮助,请告诉我,今天晚些时候,当我有另一台计算机时,我会再次检查。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-25
    • 2013-04-21
    • 2013-08-16
    • 2017-05-05
    • 1970-01-01
    • 2021-10-02
    • 2021-05-23
    • 2019-10-24
    相关资源
    最近更新 更多