-
Notifications
You must be signed in to change notification settings - Fork 0
/
ViewController.m
executable file
·115 lines (91 loc) · 3.37 KB
/
ViewController.m
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//
// ViewController.m
// PersistMe
//
// Created by Eivind Bakke on 2/16/14.
// Copyright (c) 2014 HALEALEI. All rights reserved.
//
#import "ViewController.h"
#import "AppDelegate.h"
@interface ViewController ()
{
NSManagedObjectContext *context;
}
@end
@implementation ViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[[self firstname]setDelegate:self];
[[self lastname]setDelegate:self];
AppDelegate *appdelegate = [[UIApplication sharedApplication] delegate];
context = [appdelegate managedObjectContext];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
return [textField resignFirstResponder];
}
- (IBAction)addPersonButton:(id)sender {
NSEntityDescription *entitydesc = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:context];
NSManagedObject *newPerson = [[NSManagedObject alloc]initWithEntity:entitydesc insertIntoManagedObjectContext:context];
[newPerson setValue:self.firstname.text forKey:@"firstname"];
[newPerson setValue:self.lastname.text forKey:@"lastname"];
NSError *error;
[context save:&error];
self.label.text = @"Person Added.";
}
- (IBAction)searchPersonButton:(id)sender {
NSEntityDescription *entitydesc = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc]init];
[request setEntity:entitydesc];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"firstname like %@ and lastname like %@", self.firstname.text, self.lastname.text];
[request setPredicate:predicate];
NSError *error;
NSArray *matchingData = [context executeFetchRequest:request error:&error];
if(matchingData.count <= 0) {
self.label.text=@"No Person Found.";
}
else
{
NSString *firstName;
NSString *lastName;
for (NSManagedObject *obj in matchingData) {
firstName = [obj valueForKey:@"firstname"];
lastName = [obj valueForKey:@"lastname"];
}
self.label.text = [NSString stringWithFormat:@"Firstname: %@, Lastname: %@", firstName, lastName];
}
}
- (IBAction)deletePersonButton:(id)sender {
NSEntityDescription *entityDesc = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc]init];
[request setEntity:entityDesc];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"firstname like %@", self.firstname.text];
[request setPredicate:predicate];
NSError *error;
NSArray *matchingData = [context executeFetchRequest:request error:&error];
if(matchingData.count <= 0) {
self.label.text = @"No person deleted.";
} else {
for (NSManagedObject *obj in matchingData) {
[context deleteObject:obj];
}
self.label.text = @"All instances deleted";
[context save:&error];
}
}
@end