在 iOS 中使用核心数据映射业务对象 [英] Mapping Business Objects with Core Data in iOS

查看:14
本文介绍了在 iOS 中使用核心数据映射业务对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 iOS 新手.谁能告诉我如何将我的 BO 与 Core Data 映射,以便我可以在我的项目中重用映射.这是我的代码

I am new in iOS. Can anyone tell about how I can map my BO with Core Data so that I may reuse mapping in my project. Here is my code

- (void) saveData
{       
    CoredataAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];

    NSManagedObjectContext *context = [appDelegate managedObjectContext];

    NSManagedObject *newContact;

    newContact = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:context];

    [newContact setValue:firstName.text forKey:@"firstName"];
    [newContact setValue:lastName.text forKey:@"lastName"];
    [newContact setValue:driverLicenceno.text forKey:@"driverLicenceNumber"];
    [newContact setValue:state.text forKey:@"state"];
    [newContact setValue:phoneNO.text forKey:@"phoneNumber"];
    [newContact setValue:injuryStatus.text forKey:@"injuryStatus"];
    [newContact setValue:emailAddress.text forKey:@"emailAddress"];

    NSLog(@"fName%@",firstName.text);
    firstName.text = @"";
    NSLog(@"fName%@",firstName.text);
    lastName.text = @"";
    driverLicenceno.text = @"";
    state.text = @"";
    phoneNO.text = @"";
    injuryStatus.text = @"";
    emailAddress.text = @"";

    NSError *error;
    [context save:&error];
    status.text = @"Person saved";
}

除此之外,还有其他方法可以将我的 BO 与核心数据进行映射吗?

Is there any othere way to map my BO with coredata other than this?

推荐答案

Tahir 首先你必须创建你已经创建的 Coredata db.接下来在核心数据模型的实体面板中选择表.右键单击项目层次树中的模型.选择新文件,选择 coredata 和 NSManagedObjectSubclass 然后保存它.它为 coredata 表创建模型类,您可以在您的方法中使用它.

Tahir first you must create Coredata db which you already create. Next select table in Entity panel in coredata model.Right click model in project hirarchy tree. Select new file , select coredata and NSManagedObjectSubclass and than save it. It create model class for coredata table which you can use in your methods.

此图显示选择数据库和表以右键单击并选择 NSManagedObjectSubClass

This image show selection of db and table to right click and select NSManagedObjectSubClass

此图像显示选择的第一个核心数据而不是 NSManagedObjectSubClass,在此之后,当您单击下一步"时,它会显示面板以提供名称并将其保存在您的项目中.希望能帮到你,谢谢!

This image show selection of first coredata than NSManagedObjectSubClass and after this when you click next it show you panal to give name and save this in your project. I hope this will help you thank you!

这篇关于在 iOS 中使用核心数据映射业务对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆