-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewController.m
More file actions
59 lines (53 loc) · 1.9 KB
/
Copy pathViewController.m
File metadata and controls
59 lines (53 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//
// ViewController.m
// testCoreData
//
// Created by broy denty on 14-1-23.
// Copyright (c) 2014年 denty. All rights reserved.
//
#import "ViewController.h"
#import "ArInfo.h"
#import "AppDelegate.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize context;
- (void)viewDidLoad
{
[super viewDidLoad];
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];//这里需要引进自己项目的委托,是让全局managedObjectContext起作用。
self.context = delegate.managedObjectContext;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardHide) name:UIKeyboardDidHideNotification object:nil];
// 获取coredata中的context准备数据处理
ArInfo *arInfo = [NSEntityDescription insertNewObjectForEntityForName:@"ArInfo"inManagedObjectContext:context];
// 获取coredata中一张表的实例
// 写入数据
arInfo.myid=@"123";
arInfo.myname=@"object-c";
NSError *error = nil;
if (![context save:&error]) {
NSLog(@"%@",[error localizedDescription]);
}
// 获取数据
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]init];
// 创建数据容器
NSEntityDescription *entity = [NSEntityDescription entityForName:@"ArInfo"inManagedObjectContext:context];
[fetchRequest setEntity:entity];
// 获取coredata中的entry放入数据容器中
NSArray *fetchObject = [context executeFetchRequest:fetchRequest error:&error];
// 获取数据对象读取数据
for (NSManagedObject *info in fetchObject) {
NSLog(@"id:%@",[info valueForKey:@"myid"]);
NSLog(@"name:%@",[info valueForKey:@"myname"]);
}
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)keyboardHide
{
}
@end