Makes Monster a CoreData entity

This commit is contained in:
2020-09-12 00:55:49 -07:00
parent dd7f46f580
commit f61fdc0aba
5 changed files with 71 additions and 25 deletions

View File

@@ -9,6 +9,7 @@
#import "LibraryViewController.h"
#import "Monster.h"
#import "MonsterViewController.h"
#import "AppDelegate.h"
@interface LibraryViewController ()
@@ -20,9 +21,12 @@
- (void)viewDidLoad {
[super viewDidLoad];
AppDelegate *appDelegate = (AppDelegate*)UIApplication.sharedApplication.delegate;
NSManagedObjectContext *context = appDelegate.persistentContainer.viewContext;
// Temporary setup of allMonsters until we bind to CoreData.
Monster *pixie = [[Monster alloc] initWithJSONString:@"{\"name\":\"Pixie\"}"];
Monster *acolyte = [[Monster alloc] initWithJSONString:@"{\"name\":\"Acolyte\"}"];
Monster *pixie = [[Monster alloc] initWithJSONString:@"{\"name\":\"Pixie\"}" andContext:context];
Monster *acolyte = [[Monster alloc] initWithJSONString:@"{\"name\":\"Acolyte\"}" andContext:context];
self.allMonsters = [NSArray arrayWithObjects:acolyte, pixie, nil];
}
@@ -32,7 +36,9 @@
}
- (IBAction)addNewMonster:(id)sender {
Monster *monster = [[Monster alloc] init];
AppDelegate *appDelegate = (AppDelegate*)UIApplication.sharedApplication.delegate;
NSManagedObjectContext *context = appDelegate.persistentContainer.viewContext;
Monster *monster = [[Monster alloc] initWithContext:context];
monster.name = @"Unnamed Monster";
self.allMonsters = [self.allMonsters arrayByAddingObject:monster];
[self.monstersTable reloadData];

View File

@@ -9,6 +9,7 @@
#import "SearchViewController.h"
#import "MonsterViewController.h"
#import "Monster.h"
#import "AppDelegate.h"
@interface SearchViewController ()
@@ -21,8 +22,12 @@
- (void)viewDidLoad {
[super viewDidLoad];
Monster *pixie = [[Monster alloc] initWithJSONString:@"{\"name\":\"Pixie\"}"];
Monster *acolyte = [[Monster alloc] initWithJSONString:@"{\"name\":\"Acolyte\"}"];
AppDelegate *appDelegate = (AppDelegate*)UIApplication.sharedApplication.delegate;
NSManagedObjectContext *context = appDelegate.persistentContainer.viewContext;
Monster *pixie = [[Monster alloc] initWithContext:context];
pixie.name = @"Pixie";
Monster *acolyte = [[Monster alloc] initWithEntity:[NSEntityDescription entityForName:@"Monster" inManagedObjectContext:context] insertIntoManagedObjectContext:nil];
acolyte.name = @"Acolyte";
self.allMonsters = [NSArray arrayWithObjects:acolyte, pixie, nil];
self.foundMonsters= self.allMonsters;
}