主题:核心数据,无法从 VC 中检索/设置我的属性。最常见的错误?
通过该站点进行搜索,但无法完全得到我需要的答案(尽管有很多很好的提示),所以我想我发布这个问题,希望它能解决我几周以来遇到的问题!是的,很沮丧,你大概知道这种感觉!所以任何帮助都会很棒 - 谢谢!:-)
概览:
iOS 5.1 项目。让核心数据正常工作(在 main.h/NSLog 中测试),但我在从其他视图控制器检索和设置实体属性(数据)时遇到问题。Xcode 可以识别来自其他视图控制器的 AppDelegate 中的我的单例“AppContent”,但不能识别实体名称及其属性。
对此最常见的错误是什么?
我有一种感觉,我只是错过了在正确的位置导入一些文件等......
更多细节;
仅供参考:我正在尝试使用 Matt Campell 推荐的方法,该方法在 AppDelegate 中创建一个单例,可以在整个应用程序中使用它来处理来自任何视图控制器的 managedObjectContext,并将数据检索并保存到 Core Data,它是实体及其各自的属性。这是通过将以下两个文件导入应用程序来完成的;
应用内容.h
#import <Foundation/Foundation.h>
#import "Contest.h" // Root Entity in CD model
#import "Player.h"
@interface AppContent : NSObject
+(AppContent *)sharedContent;
@property(strong, readonly) id rootObject;
-(void)save;
-(void)rollback;
@end
应用内容.m
#import "AppContent.h"
#import <CoreData/CoreData.h>
@interface AppContent()
-(NSURL *)dataStoreURL;
@property (nonatomic, strong, readonly) NSManagedObjectModel *managedObjectModel;
@property (nonatomic, strong, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;
@property (nonatomic, strong, readonly) NSManagedObjectContext *managedObjectContext;
@end
@implementation AppContent
NSManagedObjectModel *_managedObjectModel;
NSPersistentStoreCoordinator *_persistentStoreCoordinator;
NSManagedObjectContext *_managedObjectContext;
id _rootObject;
static AppContent *singletonInstance = nil;
+ (AppContent *)sharedContent{
@synchronized(self){
if (singletonInstance == nil)
singletonInstance = [[self alloc] init];
return(singletonInstance);
}
}
- (NSURL *)dataStoreURL {
NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
return [NSURL fileURLWithPath:[docDir stringByAppendingPathComponent:@"DataStore.sql"]];
}
- (NSManagedObjectModel *)managedObjectModel {
if (_managedObjectModel) {
return _managedObjectModel;
}
_managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
return _managedObjectModel;
}
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if (_persistentStoreCoordinator) {
return _persistentStoreCoordinator;
}
NSError *error = nil;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:[self dataStoreURL]
options:nil
error:&error]) {
NSLog(@"Unresolved Core Data error with persistentStoreCoordinator: %@, %@", error, [error userInfo]);
}
return _persistentStoreCoordinator;
}
- (NSManagedObjectContext *)managedObjectContext {
if (_managedObjectContext) {
return _managedObjectContext;
}
if ([self persistentStoreCoordinator]) {
_managedObjectContext = [[NSManagedObjectContext alloc] init];
[_managedObjectContext setPersistentStoreCoordinator:[self persistentStoreCoordinator]];
}
return _managedObjectContext;
}
-(id)rootObject{
if(_rootObject)
return _rootObject;
// #warning Replace [CHANGE] with your root object entity name
NSString *entityName = @"Contest";
NSManagedObjectContext *context = [self managedObjectContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:entityName
inManagedObjectContext:context];
request.entity = entity;
NSArray *listOfObjects = [context executeFetchRequest:request
error:nil];
if([listOfObjects count] == 1){
_rootObject = [listOfObjects lastObject];
return _rootObject;
}
_rootObject = [NSEntityDescription insertNewObjectForEntityForName:entityName
inManagedObjectContext:context];
// Adding some testdata (only first time...)
// Contest
Contest *c = _rootObject;
c.name = @"Big Game 1";
// Players
Player *p1 = (Player *) [NSEntityDescription insertNewObjectForEntityForName:@"Player"
inManagedObjectContext:context];
p1.name = @"Player One";
[c addPlayersObject:p1];
Player *p2 = (Player *) [NSEntityDescription insertNewObjectForEntityForName:@"Player"
inManagedObjectContext:context];
p2.name = @"Player Two";
[c addPlayersObject:p2];
[self save];
return _rootObject;
}
-(void)save{
NSError *error = nil;
NSManagedObjectContext *context = [self managedObjectContext];
if([context hasChanges])
[context save:&error];
if(error)
NSLog(@"Warning: Error saving to data store. %@", error);
}
-(void)rollback{
NSManagedObjectContext *context = [self managedObjectContext];
if([context hasChanges])
[context rollback];
}
@end
以下是模型文件(由 CD 模型编辑器创建);
竞赛.h
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@class Player;
@interface Contest : NSManagedObject
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSSet *players;
@end
@interface Contest (CoreDataGeneratedAccessors)
- (void)addPlayersObject:(Player *)value;
- (void)removePlayersObject:(Player *)value;
- (void)addPlayers:(NSSet *)values;
- (void)removePlayers:(NSSet *)values;
@end
竞赛.m
#import "Contest.h"
#import "Player.h"
@implementation Contest
@dynamic name;
@dynamic players;
@end
播放器.h
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@class Contest;
@interface Player : NSManagedObject
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) Contest *contests;
@end
播放器.m
#import "Player.h"
#import "Contest.h"
@implementation Player
@dynamic name;
@dynamic contests;
@end
这是一个视图控制器
试图在这里获取核心数据实体及其属性,但 Xcode 看到的是 appContent 但无法识别它,并且在构建它时会出现错误“找不到属性播放器”......
竞赛播放器VC.h
#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
#import "AppContent.h"
@interface contestPlayerVC : UIViewController
@property (strong, nonatomic) IBOutlet UITextField *playerNameField;
@property (strong, nonatomic) IBOutlet UITextField *playerMailField;
@property (strong, nonatomic) IBOutlet UIButton *playerSaveButton;
@property (weak, nonatomic) AppContent *content;
// Test
// @property (nonatomic, strong) NSManagedObjectContext *managedObjectContext;
- (IBAction)playerChooseImage:(UIButton *)sender;
- (IBAction)dismissModal:(UIButton *)sender;
- (IBAction)hideKeyboard:(id)sender;
- (IBAction)playerSave:(UIButton *)sender;
@end
matchPlayerVC.m 不显示孔文件,因为问题出在方法 viewDidLoad...
#import "contestPlayerVC.h"
@interface contestPlayerVC ()
@end
@implementation contestPlayerVC
@synthesize playerNameField, playerMailField, playerSaveButton, content;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
// Call 1 - seems to work ok
self.content = [AppContent sharedContent];
////// Call 2 - gives error, can't find property "player" - WHY?
// Trying to set textfield to CD Entity "Player" and it's property "name"
self.playerNameField.text = self.content.player.name;
NSLog(@"Player is: %@", self.content.player.name);
// Call 3 - works ok
NSLog(@"Content is: %@", self.content.description);
}
...
有什么提示或建议吗?谢谢!:-)
顺便说一句:很抱歉,这篇文章很长,我不确定还有什么方法可以很好地描述我的问题,让任何人都能理解。如果您阅读了所有内容 - 我印象深刻,如果您能帮我解决这个问题,我会非常高兴。可能我完全错过了一些基本的东西。谢谢 ;-)