CoreData与Objective-C跨平台开发指南
立即解锁
发布时间: 2025-08-13 01:22:56 阅读量: 20 订阅数: 38 

# Core Data与Objective-C跨平台开发指南
## 1. Core Data的使用
### 1.1 创建NSManagedObject子类
要使用Core Data,首先需要创建NSManagedObject子类。具体操作步骤如下:
1. 保持所有实体处于选中状态,依次点击“File” > “New” > “File”。
2. 在弹出的窗口中,选择“iOS” > “Core Data” > “NSManagedObject subclass”。
3. 点击“Next”,然后点击“Create”。此时会弹出一个警告对话框,提示你即将覆盖之前的Project类文件,由于确实需要更新该文件,所以点击“Replace”。
完成上述操作后,就可以在不破坏应用程序的情况下使用Core Data了。
### 1.2 代码示例
以下是一系列相关代码示例:
#### 1.2.1 AppDelegate.h
```objective-c
#import <UIKit/UIKit.h>
#import "AppModel.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
```
#### 1.2.2 AppDelegate.m
```objective-c
#import "AppDelegate.h"
@implementation AppDelegate
@synthesize window = _window;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
//Create a new AppModel instance
AppModel *dataModel = [[AppModel alloc] init];
//Make a project
Project *pl = [dataModel makeNewProject];
pl.name = @"Projl";
//Make a task
Task *t1 = (Task *)[NSEntityDescription insertNewObjectForEntityForName:@"Task" inManagedObjectContext:[dataModel managedObjectContext]];
t1.name = @"Task 1";
t1.details = @"Task details";
t1.dueDate = [NSDate date];
t1.priority = [NSNumber numberWithInt:1];
//Assign a worker to this task:
Worker *managedWorker = (Worker *)[NSEntityDescription insertNewObjectForEntityForName:@"Worker" inManagedObjectContext:[dataModel managedObjectContext]];
managedWorker.name = @"John";
managedWorker.Role = @"Programmer";
t1.assignedTo = managedWorker;
//Add the task to the project
[pl addListOfTasksObject:t1];
//Make a task
Task *t2 = (Task *)[NSEntityDescription insertNewObjectForEntityForName:@"Task" inManagedObjectContext:[dataModel managedObjectContext]];
t2.name = @"Task 2";
t2.details = @"Task details";
t2.dueDate = [NSDate date];
t2.priority = [NSNumber numberWithInt:1];
//Add the task to the project
[pl addListOfTasksObject:t2];
//Get all the projects in the data store
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Project" inManagedObjectContext:[dataModel managedObjectContext]];
request.entity = entity;
NSArray *listOfProjects = [[dataModel managedObjectContext] executeFetchRequest:request error:nil];
//print out contents of all the projects (including the tasks):
NSLog(@"-----");
NSLog(@"NEW PROJECTS IN CONTEXT");
[listOfProjects enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSLog(@"project.name = %@", [obj name]);
[[obj listOfTasks] enumerateObjectsUsingBlock:^(id obj, BOOL *stop) {
NSLog(@" task.name = %@", [obj name]);
NSLog(@" task.assignedTo = %@", [[obj assignedTo] name]);
}];
}];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
@end
```
#### 1.2.3 AppModel.h
```objective-c
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
#import "Project.h"
#import "Worker.h"
#import "Task.h"
@interface AppModel : NSObject
-(NSURL *)dataStoreURL;
@property (nonatomic, strong, readonly) NSManagedObjectModel *managedObjectModel;
@property (nonatomic, strong, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;
@property (nonatomic, strong, readonly) NSManagedObjectContext *managedObjectContext;
-(Project *)makeNewProject;
-(Worker *)makeNewWorker;
-(Task *)makeNewTask;
@end
```
#### 1.2.4 AppModel.m
```objective-c
#import "AppModel.h"
@implementation AppModel
NSManagedObjectModel *_managedObjectModel;
NSPersistentStoreCoordinator *_persistentStoreCoordinator;
NSManagedObjectContext *_managedObjectContext;
-(Project *)makeNewProject{
Project *managedProject = (Project *)[NSEntityDescription insertNewObjectForEntityForName:@"Project" inManagedObjectContext:[self managedObjectContext]];
managedProject.name = @"New Project";
managedProject.descrip = @"This is a new project";
managedProject.dueDate = [NSDate date];
managedProject.personInCharge = [self makeNewWorker];
return managedProject;
}
-(Worker *)makeNewWorker{
Worker *managedWorke
```
0
0
复制全文
相关推荐









