0

Hi based on a tutorial I have wired following method to Done button in Add New view controller.

The text field saves back to its own controller. The program builds and runs. But when you click the Done button to save the new record, it throws an error. The method seems to get the text entered--based on the presence of name NSString * @"jkl" 0x00007f9828d8d6b0 in the debugger but the NS entity is nil and it crashes.entity NSEntityDescription * nil 0x0000000000000000 I've triple checked the name of the entity and it is correct.

Can anyone tell me what might be going wrong? Thanks!

- (IBAction)save:(id)sender {
    // Helpers
    NSString *name = self.textField.text;
  [[[UIAlertView alloc] initWithTitle:@"We got data" message:name delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
    if (name && name.length) {
        // Create Entity FOLLOWING LINE IS HIGHLIGHTED AND THROWS ERROR
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"TSPItem" inManagedObjectContext:self.managedObjectContext];
 //END HIGHLIGHTED ERRROR       
        // Initialize Record
        NSManagedObject *record = [[NSManagedObject alloc] initWithEntity:entity insertIntoManagedObjectContext:self.managedObjectContext];

        // Populate Record
        [record setValue:name forKey:@"name"];
        [record setValue:[NSDate date] forKey:@"createdAt"];

        // Save Record
        NSError *error = nil;

        if ([self.managedObjectContext save:&error]) {
            // Dismiss View Controller
            [self dismissViewControllerAnimated:YES completion:nil];

        } else {
            if (error) {
                NSLog(@"Unable to save record.");
                NSLog(@"%@, %@", error, error.localizedDescription);
            }

            // Show Alert View
            [[[UIAlertView alloc] initWithTitle:@"Warning" message:@"Your field could not be saved." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
        }

    } else {
        // Show Alert View
        [[[UIAlertView alloc] initWithTitle:@"Warning" message:@"Your field needs a name." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
    }

Edit:

Here is some additional stuff from one of the threads. I am not skilled enough at debugging to know what it means:

0x10ce9de77:  leaq   0x26fa82(%rip), %r8       ; @"+entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name '%@'"
0x10ce9de7e:  jmp    0x10ce9dece               ; +[NSEntityDescription entityForName:inManagedObjectContext:] + 206
0x10ce9de80:  movq   0x2c5311(%rip), %r14      ; (void *)0x000000010dce1458: NSException
0x10ce9de87:  movq   0x2691ca(%rip), %rax      ; (void *)0x000000010dca9a20: NSInvalidArgumentException
0x10ce9de8e:  movq   (%rax), %r15
0x10ce9de91:  movq   0x2c52b8(%rip), %rcx      ; (void *)0x000000010d585e90: NSString
0x10ce9de98:  movq   0x2be8e9(%rip), %rdx      ; "stringWithFormat:"
0x10ce9de9f:  leaq   0x26fa7a(%rip), %r8       ; @"+entityForName: nil is not a legal NSPersistentStoreCoordinator for searching for entity name '%@'"
0x10ce9dea6:  jmp    0x10ce9dece               ; +[NSEntityDescription entityForName:inManagedObjectContext:] + 206
0x10ce9dea8:  movq   0x2c52e9(%rip), %r14      ; (void *)0x000000010dce1458: NSException
0x10ce9deaf:  movq   0x26919a(%rip), %rax      ; (void *)0x000000010dca9a28: NSInternalInconsistencyException
0x10ce9deb6:  movq   (%rax), %r15
0x10ce9deb9:  movq   0x2c5290(%rip), %rcx      ; (void *)0x000000010d585e90: NSString
0x10ce9dec0:  movq   0x2be8c1(%rip), %rdx      ; "stringWithFormat:"
0x10ce9dec7:  leaq   0x26fa72(%rip), %r8       ; @"+entityForName: could not locate an NSManagedObjectModel for entity name '%@'"
0x10ce9dece:  movq   0x2692eb(%rip), %r12      ; (void *)0x000000010d6e9000: objc_msgSend
0x10ce9ded5:  xorl   %eax, %eax
0x10ce9ded7:  movq   %rcx, %rdi
0x10ce9deda:  movq   %rdx, %rsi
0x10ce9dedd:  movq   %r8, %rdx
0x10ce9dee0:  movq   %rbx, %rcx
0x10ce9dee3:  callq  *%r12
0x10ce9dee6:  movq   0x2be55b(%rip), %rcx      ; "exceptionWithName:reason:userInfo:"
user1904273
  • 4,562
  • 11
  • 45
  • 96
  • Is self.managedObjectContext nil? – Mike Taverne Feb 03 '15 at 02:18
  • see edit. I feel like I the core data stack is not accessible from this controller but I don't understand what is really going on well enough to sort it out. – user1904273 Feb 03 '15 at 15:51
  • It's telling you that your managedObjectContext is nil. Without that, you can't do anything with NSEntityDescription. Where is managedObjectContext defined in your code and how is getting initialized? – Mike Taverne Feb 03 '15 at 16:24
  • It is in IDModel (following book tutorial). - (NSManagedObjectContext *)managedObjectContext { if (_managedObjectContext != nil) { return _managedObjectContext; } In turn, ID Model is self instantiated as a singleton with following lines (I think) static IDModel *_sharedInstance = nil; + (IDModel*)sharedInstance { if(!_sharedInstance) { _sharedInstance=[[IDModel alloc] init]; } return _sharedInstance; } – user1904273 Feb 03 '15 at 17:10

0 Answers0