0

我在核心数据方面相当新,sry。我的核心数据实体有一个派生自 NSManagedObject 的子类“CachedWeatherDataEntity”。我在我的协议“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

缓存天气数据实体.m

#import "CachedWeatherDataEntity.h"


@implementation CachedWeatherDataEntity

@dynamic airHumidity;
@dynamic airPressure;
@dynamic dateTime;
@dynamic illuminance;
@dynamic precipitation;
@dynamic radiation1;
@dynamic radiation2;
@dynamic sunDown;
@dynamic sunUp;
@dynamic temperature;
@dynamic windDirection;
@dynamic windSpeed;

@end

WeatherDataCache.h

#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

WeatherDataCacheImpl.h

#import  <Foundation/Foundation.h>
#import "WeatherDataCache.h"
#import "WeatherDataSetImpl.h"

@interface WeatherDataCacheImpl : NSObject <WeatherDataCache> {

}
@end

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);
    }
}
4

1 回答 1

0

这里至少有一个问题:

CachedWeatherDataEntity *CachedWeatherDataEntity = ...

因为局部变量与类同名。您应该重命名变量,例如小写:

CachedWeatherDataEntity *cachedWeatherDataEntity = ...
于 2013-09-02T16:40:30.207 回答