Ios 使用未声明的标识符核心数据实体子类(NSManaged对象)

Ios 使用未声明的标识符核心数据实体子类(NSManaged对象),ios,objective-c,core-data,nsmanagedobject,Ios,Objective C,Core Data,Nsmanagedobject,我在核心数据方面相当新,sry。我的核心数据实体有一个子类“CachedWeatherDataEntity”,它派生自NSManagedObject。我导入协议“WeatherDataCache”中的“CachedWeatherDataEntity.h”-文件。该协议定义了保存/获取数据的方法,并将核心数据设置为“核心功能”。但是现在编译器在相应的实现“WeatherDataCacheImpl”中找不到我的实体。错误在代码的末尾。此应用程序基于 CachedWeatherDataEntity.h

我在核心数据方面相当新,sry。我的核心数据实体有一个子类“CachedWeatherDataEntity”,它派生自NSManagedObject。我导入协议“WeatherDataCache”中的“CachedWeatherDataEntity.h”-文件。该协议定义了保存/获取数据的方法,并将核心数据设置为“核心功能”。但是现在编译器在相应的实现“WeatherDataCacheImpl”中找不到我的实体。错误在代码的末尾。此应用程序基于

CachedWeatherDataEntity.h

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


@interface CachedWeatherDataEntity : NSManagedObject

@property (nonatomic, retain) NSNumber * airHumidity;
@property (nonatomic, retain) NSNumber * airPressure;
@property (nonatomic, retain) NSDate * dateTime;
@property (nonatomic, retain) NSNumber * illuminance;
@property (nonatomic, retain) NSNumber * precipitation;
@property (nonatomic, retain) NSNumber * radiation1;
@property (nonatomic, retain) NSNumber * radiation2;
@property (nonatomic, retain) NSDate * sunDown;
@property (nonatomic, retain) NSDate * sunUp;
@property (nonatomic, retain) NSNumber * temperature;
@property (nonatomic, retain) NSNumber * windDirection;
@property (nonatomic, retain) NSNumber * windSpeed;

@end
#import <Foundation/Foundation.h>
#import "WeatherDataSet.h"
#import "CachedWeatherDataEntity.h"

@protocol WeatherDataCache <NSObject>

@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;

- (id<WeatherDataSet>) getData:(NSDate*)pDateTime;

- (void) saveDataArray:(NSMutableArray*)pDataArray;

- (void) saveData:(id<WeatherDataSet>)pData;

- (void)saveContext;

- (NSURL *)applicationDocumentsDirectory;
@end
#import  <Foundation/Foundation.h>
#import "WeatherDataCache.h"
#import "WeatherDataSetImpl.h"

@interface WeatherDataCacheImpl : NSObject <WeatherDataCache> {

}
@end
WeatherDataCache.h

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


@interface CachedWeatherDataEntity : NSManagedObject

@property (nonatomic, retain) NSNumber * airHumidity;
@property (nonatomic, retain) NSNumber * airPressure;
@property (nonatomic, retain) NSDate * dateTime;
@property (nonatomic, retain) NSNumber * illuminance;
@property (nonatomic, retain) NSNumber * precipitation;
@property (nonatomic, retain) NSNumber * radiation1;
@property (nonatomic, retain) NSNumber * radiation2;
@property (nonatomic, retain) NSDate * sunDown;
@property (nonatomic, retain) NSDate * sunUp;
@property (nonatomic, retain) NSNumber * temperature;
@property (nonatomic, retain) NSNumber * windDirection;
@property (nonatomic, retain) NSNumber * windSpeed;

@end
#import <Foundation/Foundation.h>
#import "WeatherDataSet.h"
#import "CachedWeatherDataEntity.h"

@protocol WeatherDataCache <NSObject>

@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;

- (id<WeatherDataSet>) getData:(NSDate*)pDateTime;

- (void) saveDataArray:(NSMutableArray*)pDataArray;

- (void) saveData:(id<WeatherDataSet>)pData;

- (void)saveContext;

- (NSURL *)applicationDocumentsDirectory;
@end
#import  <Foundation/Foundation.h>
#import "WeatherDataCache.h"
#import "WeatherDataSetImpl.h"

@interface WeatherDataCacheImpl : NSObject <WeatherDataCache> {

}
@end
#导入
#导入“WeatherDataSet.h”
#导入“CachedWeatherDataEntity.h”
@协议数据缓存
@属性(只读、强、非原子)NSManagedObjectContext*managedObjectContext;
@属性(只读、强、非原子)NSManagedObjectModel*managedObjectModel;
@属性(只读、强、非原子)NSPersistentStoreCoordinator*persistentStoreCoordinator;
-(id)getData:(NSDate*)pDateTime;
-(void)saveDataArray:(NSMutableArray*)pDataArray;
-(void)saveData:(id)pData;
-(无效)保存上下文;
-(NSURL*)申请文件目录;
@结束
WeatherDataCacheImpl.h

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


@interface CachedWeatherDataEntity : NSManagedObject

@property (nonatomic, retain) NSNumber * airHumidity;
@property (nonatomic, retain) NSNumber * airPressure;
@property (nonatomic, retain) NSDate * dateTime;
@property (nonatomic, retain) NSNumber * illuminance;
@property (nonatomic, retain) NSNumber * precipitation;
@property (nonatomic, retain) NSNumber * radiation1;
@property (nonatomic, retain) NSNumber * radiation2;
@property (nonatomic, retain) NSDate * sunDown;
@property (nonatomic, retain) NSDate * sunUp;
@property (nonatomic, retain) NSNumber * temperature;
@property (nonatomic, retain) NSNumber * windDirection;
@property (nonatomic, retain) NSNumber * windSpeed;

@end
#import <Foundation/Foundation.h>
#import "WeatherDataSet.h"
#import "CachedWeatherDataEntity.h"

@protocol WeatherDataCache <NSObject>

@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;

- (id<WeatherDataSet>) getData:(NSDate*)pDateTime;

- (void) saveDataArray:(NSMutableArray*)pDataArray;

- (void) saveData:(id<WeatherDataSet>)pData;

- (void)saveContext;

- (NSURL *)applicationDocumentsDirectory;
@end
#import  <Foundation/Foundation.h>
#import "WeatherDataCache.h"
#import "WeatherDataSetImpl.h"

@interface WeatherDataCacheImpl : NSObject <WeatherDataCache> {

}
@end
#导入
#导入“WeatherDataCache.h”
#导入“WeatherDataSetImpl.h”
@接口WeatherDataCacheImpl:NSObject{
}
@结束
WeatherDataCacheImpl.m+++这里是错误+++

#import "WeatherDataCacheImpl.h"
#import "CachedWeatherDataEntity.h"


@implementation WeatherDataCacheImpl

@synthesize managedObjectContext = _managedObjectContext;
@synthesize managedObjectModel = _managedObjectModel;
@synthesize persistentStoreCoordinator = _persistentStoreCoordinator;


- (id<WeatherDataSet>) getData:(NSDate*)pDateTime {

    return nil;
}

- (void) saveDataArray:(NSMutableArray*)pDataArray {

    double temp = 23.3;
    double air = 95.0;
    NSDate *myDate = [[NSDate alloc]init];




    NSManagedObjectContext *context = [self managedObjectContext];
    CachedWeatherDataEntity *CachedWeatherDataEntity = [NSEntityDescription
                                      insertNewObjectForEntityForName:@"CachedWeatherDataEntity"
                                      inManagedObjectContext:context];
    CachedWeatherDataEntity.temperature = [NSNumber numberWithDouble:temp];
    CachedWeatherDataEntity.airHumidity = [NSNumber numberWithDouble:air];
    CachedWeatherDataEntity.dateTime = myDate;



    NSError *error;
    if (![context save:&error]) {
        NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
    }

    // Test listing all CachedWeatherDataEntitys from the store
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"CachedWeatherDataEntity"
                                              inManagedObjectContext:context];
    [fetchRequest setEntity:entity];
    NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];



    for (CachedWeatherDataEntity *info in fetchedObjects) { <-----------Use of undeclared identifier 'info'

        NSLog(@"temp: %@", info.temp);        
        NSLog(@"air: %@", info.air);
    }
}
#导入“WeatherDataCacheImpl.h”
#导入“CachedWeatherDataEntity.h”
@WeatherDataCacheImpl的实现
@综合managedObjectContext=_managedObjectContext;
@综合managedObjectModel=_managedObjectModel;
@合成persistentStoreCoordinator=\u persistentStoreCoordinator;
-(id)getData:(NSDate*)pDateTime{
返回零;
}
-(void)saveDataArray:(NSMutableArray*)pDataArray{
双温=23.3;
双空气=95.0;
NSDate*myDate=[[NSDate alloc]init];
NSManagedObjectContext*上下文=[self-managedObjectContext];
CachedWeatherDataEntity*CachedWeatherDataEntity=[N实体描述
insertNewObjectForEntityForName:@“CachedWeatherDataEntity”
inManagedObjectContext:context];
CachedWeatherDataEntity.temperature=[NSNumber numberWithDouble:temp];
CachedWeatherDataEntity.air湿度=[NSNumber numberWithDouble:air];
CachedWeatherDataEntity.dateTime=myDate;
n错误*错误;
如果(![上下文保存:&错误]){
NSLog(@“哎哟,无法保存:%@,[error localizedDescription]);
}
//测试列出存储区中的所有CachedWeatherDataEntity
NSFetchRequest*fetchRequest=[[NSFetchRequest alloc]init];
NSEntityDescription*entity=[NSEntityDescription entityForName:@“CachedWeatherDataEntity”
inManagedObjectContext:context];
[FetchRequestSetEntity:entity];
NSArray*fetchedObjects=[context executeFetchRequest:fetchRequest错误:&error];

对于(FetchedObject中的CachedWeatherDataEntity*信息){至少存在一个问题:

CachedWeatherDataEntity *CachedWeatherDataEntity = ...
因为局部变量与类具有相同的名称。您应该重命名 变量,例如小写:

CachedWeatherDataEntity *cachedWeatherDataEntity = ...

我通常将我的核心数据方法放在我的AppDelegate中,以便视图和控制器可以访问它们。你在苹果的开发网站上看到CoreDataBooks或ThreadedCoreData示例了吗?最后,你用这个名字称呼你的实体,但你在CachedWeather上调用*info…对吗?你试过“entity.info.temp”吗?不是舒尔。我真的应该看看这些书,因为越快我就需要实体来显示图表并放大它们以获得更多细节……是的,这很有效。以前重构太多了。这就是原因。非常感谢!