-1

I am trying to prepare for segue, but when ever I set an instance of the VC i'm going to, I get a thread 1 signal sigabrt error. I import the VC, #import "NoteViewController.h"

And in my prepare for Segue:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"ShowNote"]) {
    NoteDetailViewController *noteDetailViewController = [segue destinationViewController];
    NSIndexPath *selectedIndexPath = self.tableView.indexPathForSelectedRow;
    NSInteger arrayItem = selectedIndexPath.row * 2;
    noteDetailViewController.note = [_notes objectAtIndex:arrayItem];
} else if ([segue.identifier isEqualToString:@"AddNote"]) {
    Note *note = [Note new];
    [self.notes addObject:note];
    NoteDetailViewController *noteDetailViewController = [segue destinationViewController]; // ERROR COMES HERE!
    noteDetailViewController.note = note;
}
}

I've been debugging for the past 30 minutes, but I can't figure out whats happening. Thanks!

EDIT:

I might have found the issue;

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attempt to insert non-property list object ( "" ) for key notesData'

Here's my VC code.

#import "NotesListTableViewController.h"
#import "Note.h"
#import "NoteDetailViewController.h"

@interface NotesListTableViewController ()

@property (nonatomic, strong) NSMutableArray *notes;

@end

@implementation NotesListTableViewController

- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];

if (self) {
    _notes = [[NSUserDefaults standardUserDefaults]
              mutableArrayValueForKey:@"notesData"];;
}

return self;
}

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];

[self.tableView reloadData];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return [self.notes count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NotesCell" forIndexPath:indexPath];
       cell.textLabel.text = [_notes objectAtIndex:indexPath.row * 2];
     //  cell.textLabel.text = [self.notes[indexPath.row] title];
       cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

return cell;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

NSInteger titleToDelete = indexPath.row * 2;
NSInteger messageToDelete = titleToDelete - 1;

[_notes removeObjectAtIndex:titleToDelete];
[_notes removeObjectAtIndex:messageToDelete];
[[NSUserDefaults standardUserDefaults]setObject:_notes forKey:@"notesData"];

[self.notes removeObjectAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}



- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

if ([segue.identifier isEqualToString:@"ShowNote"]) {
    NoteDetailViewController *noteDetailViewController = [segue destinationViewController];
    NSIndexPath *selectedIndexPath = self.tableView.indexPathForSelectedRow;
    NSInteger arrayItem = selectedIndexPath.row * 2;
    noteDetailViewController.note = [_notes objectAtIndex:arrayItem];
} else if ([segue.identifier isEqualToString:@"AddNote"]) {
    Note *note = [Note new];
    [self.notes addObject:note];
    NoteDetailViewController *noteDetailViewController = [segue destinationViewController];
    noteDetailViewController.note = note;
}
}



- (IBAction)doneButton:(id)sender {

[self dismissViewControllerAnimated:YES completion:nil];

}

@end
  • What is the exception message? Which line does it crash on? – Paulw11 Apr 10 '17 at 23:53
  • If you scroll through the code, I added a note where it crashed, but it crashes on the second `NoteDetailViewController *noteDetailViewControlelr = [segue destinationViewController];` and the message is 2017-04-10 19:37:20.235616-0400 final attempt[74022:7653033] [User Defaults] Attempt to set a non-property-list object or libc++abi.dylib: terminating with uncaught exception of type NSException (lldb) I don't know which – user7847400 Apr 11 '17 at 00:08
  • It isn't crashing on that line. Set an exception breakpoint. It is crashing because you are attempting to write something to user defaults that isn't supported – Paulw11 Apr 11 '17 at 00:11
  • See my edit. I added some code. – user7847400 Apr 11 '17 at 00:16
  • If you are going to store an object in UserDefaults it needs to support NsCoding, but this look a like the sort of thing where you should use Core Data or Realm – Paulw11 Apr 11 '17 at 00:18
  • Could you expand on that? I'm a bit of a "noob" – user7847400 Apr 11 '17 at 00:30

1 Answers1

0

i think it is error from here _notes = [[NSUserDefaults standardUserDefaults] mutableArrayValueForKey:@"notesData"];

maybe you get this error,try this answer https://stackoverflow.com/a/19720674/2680768

Community
  • 1
  • 1