简体   繁体   中英

Receiver type is forward declaration

I have this code (along with other stuff):

- (NSManagedObjectContext *) managedObjectContext
{
    assert([NSThread isMainThread]);
    if (_managedObjectContext != nil) {
        return _managedObjectContext;
    }
    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (coordinator != nil) {
        _managedObjectContext = [[NSManagedObjectContext alloc] init];
        [_managedObjectContext setPersistentStoreCoordinator: coordinator];
    }

    return _managedObjectContext;
}

These lines are giving me errors that class message is a forward declaration:

_managedObjectContext = [[NSManagedObjectContext alloc] init];
[_managedObjectContext setPersistentStoreCoordinator: coordinator];

What is this and how do I fix it?

You must import CoreData/CoreData.h in the file Supporting Files/YourApp-Prefix.pch:

#ifdef __OBJC__
    #import <UIKit/UIKit.h>
    #import <Foundation/Foundation.h>
    #import <CoreData/CoreData.h>
#endif

By forward Declaration means you must be declaring class by @Class . Import the Class in the .h/.m file and hope so it will resolve the issue.

将其添加到.h或.m文件的顶部

#import <CoreData/CoreData.h>

我通过在使用Swift类的Objective C类中导入#import“Project-Name-Swift.h”文件解决了这个问题。

I Change file DDXML.h and Work.

File: DDXML.h

Change:

#if TARGET_OS_IPHONE && 0 // Disabled by default

To:

#if TARGET_OS_IPHONE && 1 // Disabled by default

这在我重命名一个类后经常发生,然后忘记在头文件中更新@class myclass(显然不会抛出编译错误。)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM