【问题标题】:Restkit 2.0 'managedObjectContext' not found on object of type在类型的对象上找不到 Restkit 2.0 'managedObjectContext'
【发布时间】:2014-02-17 05:38:32
【问题描述】:

请原谅可能的菜鸟问题。我正在使用 Restkit 2.0 构建一个应用程序,但在实现核心数据功能时遇到了问题。在this tutorial 之后,我将以下代码添加到我的 AppViewController(所有其他视图控制器都扩展了此类)。

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationController.navigationBar.shadowImage = [UIImage new];

    NSError *error = nil;
    NSURL *modelURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Jumplytics" ofType:@"momd"]];
    // NOTE: Due to an iOS 5 bug, the managed object model returned is immutable.
    NSManagedObjectModel *managedObjectModel = [[[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL] mutableCopy];
    RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];

    // Initialize the Core Data stack
    [managedObjectStore createPersistentStoreCoordinator];

    NSPersistentStore __unused *persistentStore = [managedObjectStore addInMemoryPersistentStore:&error];
    NSAssert(persistentStore, @"Failed to add persistent store: %@", error);

    [managedObjectStore createManagedObjectContexts];

    // Set the default store shared instance
    [RKManagedObjectStore setDefaultStore:managedObjectStore];

    // Override point for customization after application launch.
    self.managedObjectContext = managedObjectStore.mainQueueManagedObjectContext;
}

我收到错误:

property managedObjectContext not found on object of type 'AppViewController'

我可以做些什么来解决这个问题?

【问题讨论】:

    标签: ios core-data restkit restkit-0.20


    【解决方案1】:

    您可能不希望超类中viewDidLoad 中的这段代码,因为这意味着它正在为每个子类运行。因此,每个 VC 都会有自己的 Core Data 堆栈。您更有可能需要一个可供所有控制器使用的单一核心数据堆栈(可能在单例数据控制器中)。

    你的错误:

    在“AppViewController”类型的对象上找不到属性 managedObjectContext

    似乎与 RestKit 无关。这意味着当您没有名为managedObjectContext 的属性时,您正在使用self.managedObjectContext。您只需要添加该属性,以便它可以被引用。

    【讨论】:

    • 您可能希望从 AppViewController... 的 .h / continuation 类别中添加更多代码...
    • 哦,呵呵。该错误肯定会发生,因为我没有在头文件中定义该属性。在 viewDidLoad 中没有它是有道理的——那么在 AppDelegate 中初始化会更好吗?
    猜你喜欢
    • 2012-06-30
    • 2014-04-27
    • 2012-03-04
    • 1970-01-01
    • 2013-06-10
    • 1970-01-01
    • 2021-02-12
    • 2023-03-22
    • 2019-06-25
    相关资源
    最近更新 更多