Iphone 核心数据在我的控制器中不工作。如何将managedObjectContext链接到我的控制器?
我第一次使用核心数据概念来简单地插入和检索一些信息 我有Iphone 核心数据在我的控制器中不工作。如何将managedObjectContext链接到我的控制器?,iphone,core-data,Iphone,Core Data,我第一次使用核心数据概念来简单地插入和检索一些信息 我有userInfoAppDelegate.h,userInfoAppDelegate.m和DisplayController.h,DisplayController.m 现在我已经成功地在我的委托文件中添加和检索了数据,下面是代码 userInfoAppDelegate.h #import <UIKit/UIKit.h> #import "HomeController.h" @class RootViewController;
userInfoAppDelegate.h
,userInfoAppDelegate.m
和DisplayController.h
,DisplayController.m
现在我已经成功地在我的委托文件中添加和检索了数据,下面是代码
userInfoAppDelegate.h
#import <UIKit/UIKit.h>
#import "HomeController.h"
@class RootViewController;
@interface UserInfoAppDelegate : UIResponder <UIApplicationDelegate>{
HomeController *hController;
UIWindow *Window;
}
@property (nonatomic, strong) UINavigationController *navigationController;
@property (nonatomic, strong) RootViewController *rootViewController;
@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
- (void)saveContext;
- (NSURL *)applicationDocumentsDirectory;
@end
//上述代码运行良好,结果正常,但同一代码不适用于MY
DisplayController.h
和DisplayController.m
在控制器中获取AppDelegate引用,然后在使用self.managedObjectContext
的地方,使用delegate.managedObjectContext
这是一个小样本
userInfoAppDelegate *delegate = (userInfoAppDelegate *)[[UIApplication sharedApplication] delegate];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"UserInfo"
inManagedObjectContext:delegate.managedObjectContext];
如果需要DisplayController代码,请告诉我。我是否需要将UserInfoAppDelegate.h导入我的控制器,或者它可以从所有控制器访问?您需要将它导入到您想要使用它的位置!虽然有点不对劲。有人能解释这是什么意思吗<代码>N错误*savingError=nil;if([self.managedObjectContext save:&savingError]){NSLog(@“成功保存了上下文”);}else{NSLog(@“未能保存上下文。错误=%@”,savingError);}if(![self.managedObjectContext save:&savingError])添加“!”:)我已成功插入和检索数据,但我可以看到这些数据存储在哪里吗?我的数据模型文件是UserInfo.xcdatamodeld。
userInfoAppDelegate *delegate = (userInfoAppDelegate *)[[UIApplication sharedApplication] delegate];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"UserInfo"
inManagedObjectContext:delegate.managedObjectContext];