[英]iOS CoreData can't get the property using objective-c
I try to using objective-c write the core data. 我尝试使用Objective-C编写核心数据。
My swift code can get the User Entites like below 我的快速代码可以获取如下的用户实体
swift3: swift3:
let appDel = UIApplication.shared.delegate as? AppDelegate;
guard let context = appDel?.persistentContainer.viewContext else{ return }
let User = User(context: context);
I using objective-c write in the viewdidload. 我使用objective-c在viewdidload中编写。
But I can't persistentContainer property. 但是我不能persistentContainer属性。
like below: 如下所示:
Objective-c Objective-C的
UIApplication *application = [UIApplication sharedApplication];
NSManagedObjectContext *context = (AppDelegate*)(application.delegate).
persistentContainer.viewContext;
Have anyone can resolve the problem about can't get persistentContainer property? 有谁能解决关于无法获得persistentContainer属性的问题?
I using the Xcode8.2.1 objective-c project. 我使用Xcode8.2.1 Objective-C项目。
Thank you very much. 非常感谢你。
*First we should import core data framework in Appdelegate and set the property of persistentContainer In Appdelegate.h file *首先,我们应该在Appdelegate中导入核心数据框架,并在Appdelegate.h文件中设置persistentContainer的属性
@property (readonly, strong) NSPersistentContainer *persistentContainer;
- (void)saveContext;
In Appdelegate.m file 在Appdelegate.m文件中
@synthesize persistentContainer = _persistentContainer;
- (NSPersistentContainer *)persistentContainer {
// The persistent container for the application. This implementation creates and returns a container, having loaded the store for the application to it.
@synchronized (self) {
if (_persistentContainer == nil) {
_persistentContainer = [[NSPersistentContainer alloc] initWithName:@"NSURL"];
[_persistentContainer loadPersistentStoresWithCompletionHandler:^(NSPersistentStoreDescription *storeDescription, NSError *error) {
if (error != nil) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
/*
Typical reasons for an error here include:
* The parent directory does not exist, cannot be created, or disallows writing.
* The persistent store is not accessible, due to permissions or data protection when the device is locked.
* The device is out of space.
* The store could not be migrated to the current model version.
Check the error message to determine what the actual problem was.
*/
NSLog(@"Unresolved error %@, %@", error, error.userInfo);
abort();
}
}];
}
}
return _persistentContainer;
}
#pragma mark - Core Data Saving support
- (void)saveContext {
NSManagedObjectContext *context = self.persistentContainer.viewContext;
NSError *error = nil;
if ([context hasChanges] && ![context save:&error]) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(@"Unresolved error %@, %@", error, error.userInfo);
abort();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.