我是ios的新手。我正在努力学习ios,但直到现在我还没有成功在ios中做CRUD。
我正在按照教程进行操作,代码中没有问题,您将看到下面的代码。
NSObject
子类。UIVIEWCONTROLLER
的子类。主要问题是我没有CORE DATA db文件告诉我正在插入数据......
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (_persistentStoreCoordinator != nil) {
return _persistentStoreCoordinator;
}
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"CrudWithInterface.sqlite"];
在哪里找到此文件 CrudWithInterface.sqlite
保存按钮事件
- (IBAction)btnAddPerson:(id)sender {
NSEntityDescription *entityDesc = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:context];
NSManagedObject *newPerson = [[NSManagedObject alloc]initWithEntity:entityDesc insertIntoManagedObjectContext:context];
[newPerson setValue:self.txtFirstName.text forKey:@"firstName"];
[newPerson setValue:self.txtFirstName.text forKey:@"lastName"];
[newPerson setValue:self.txtFirstName.text forKey:@"address"];
NSError *error;
[context save:&error];
self.fldDisplayData.text = @"person Added Successfully... :)";
}
我也搜索了路径〜/ User / Library / SharedAllication .....
那里没有IOS模拟器文件夹......
答案 0 :(得分:0)
试试这个:
步骤1:我没有看到持久性存储协调器的所有代码。如果您想要多次运行应用程序,则必须阅读或创建NSPersistentStoreCoordinator。
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (_persistentStoreCoordinator != nil) {
return _persistentStoreCoordinator;
}
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"CrudWithInterface.sqlite"];
NSError *error = nil;
_persistentStoreCoordinator = [[[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:<your managed Obbject Model>] retain];
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error])
{
NSLog(@"Suppression du model de données !!");
[[NSFileManager defaultManager] removeItemAtURL:storeURL error:NULL];
[_persistentStoreCoordinator release];
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:<your managed Obbject Model>];
if (![singleton addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error])
{
abort();
}
}
return _persistentStoreCoordinator;
}
步骤2:现在保存按钮(我现在不知道如何创建您的managedObjectContext命名上下文) 你可以使用静态方法“NSEntityDescription insertNewObjectForEntityForName”它更有用
- (IBAction)btnAddPerson:(id)sender {
//Normaly you subclass NSManagedObject. On your modele file Editor menu "Create NSManaged subclass"
NSManagedObject *newPerson = [NSEntityDescription insertNewObjectForEntityForName:@"Person"
inManagedObjectContext: context];
[newPerson setValue:self.txtFirstName.text forKey:@"firstName"];
[newPerson setValue:self.txtFirstName.text forKey:@"lastName"];
[newPerson setValue:self.txtFirstName.text forKey:@"address"];
NSError *error;
if(![context save:&error])
{
DLog(@"Whoops, couldn't save: %@", [err localizedDescription]);
}
self.fldDisplayData.text = @"person Added Successfully... :)";
}