Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 第二次添加/删除断言失败_Ios_Objective C_Ipad_Uitableview_Nsexception - Fatal编程技术网

Ios 第二次添加/删除断言失败

Ios 第二次添加/删除断言失败,ios,objective-c,ipad,uitableview,nsexception,Ios,Objective C,Ipad,Uitableview,Nsexception,我有一个奇怪的问题:我在一个装有按钮的SplitViewController中有一个DetailViewController。此按钮具有指向NavigationController和UITableViewController(用作记事本)的Popover序列。我第一次打开popover时,记事本工作得非常好,正如预期的那样。但是,如果我关闭popover,然后重新打开它,则在添加或删除便笺时它将失败。应用程序重新启动后,功能将恢复 添加代码: [_objects insertObject:new

我有一个奇怪的问题:我在一个装有按钮的SplitViewController中有一个DetailViewController。此按钮具有指向NavigationController和UITableViewController(用作记事本)的Popover序列。我第一次打开popover时,记事本工作得非常好,正如预期的那样。但是,如果我关闭popover,然后重新打开它,则在添加或删除便笺时它将失败。应用程序重新启动后,功能将恢复

添加代码:

[_objects insertObject:newNote atIndex:0];
[_objects removeObjectAtIndex:indexPath.row];
删除代码:

[_objects insertObject:newNote atIndex:0];
[_objects removeObjectAtIndex:indexPath.row];
几乎整个NotesViewController.m类:

#import "NotesTableViewController.h"

@interface NotesTableViewController ()
{
NSMutableArray *_objects;
BOOL firstRun;
}

@end

@implementation NotesTableViewController
@synthesize openSubject;

- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
    // Custom initialization
}
return self;
}

- (void)awakeFromNib
{
self.clearsSelectionOnViewWillAppear = NO;
self.preferredContentSize = CGSizeMake(320.0, 600.0);
[super awakeFromNib];
}

- (void)viewDidLoad
{
[super viewDidLoad];

// Load the notes data
// Create the key
NSString *partOfKey = @"-notes";
NSString *notesKey = [NSString stringWithFormat:@"%@%@", openSubject, partOfKey];

// Do any additional setup after loading the view, typically from a nib
self.navigationItem.leftBarButtonItem = self.editButtonItem;

// Register a class or nib file using registerNib:forCellReuseIdentifier
// o registerClass:forCellReuiseIdentifier: method before calling this method
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];

// Load the _objects from NSUserdefaults
_objects = nil;
_objects = [[NSUserDefaults standardUserDefaults] objectForKey:notesKey];

if (openSubject.length == 0) {
    // There's currently no subject open, write it in the navigationbar
    // Prompt = open subject
    // Title = notes header
    self.navigationItem.prompt = @"No subject selected";
    self.navigationItem.title = @"My Notes";
} else {
    // Open the subject

    // Prompt = notes header
    // Title = open subject
    self.navigationItem.prompt = openSubject;
    self.navigationItem.title = @"My notes";
}

// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
}



- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)addNote:(id)sender {
// Create a new note
if (openSubject.length == 0) {
    // The openSubject is nil, can't add a subject - tell the user
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"No subject" message: @"Please select a subject prior to adding a note" delegate: nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show];
} else {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"New note" message:@"Enter a note" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Add", nil];
    alert.alertViewStyle = UIAlertViewStylePlainTextInput;
    [alert show];
}
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
// The user created a new subject, add it
if (buttonIndex == 1) {
    // Get the input text
    NSString *newNote = [[alertView textFieldAtIndex:0] text];

    // Check if the note already exist
    if ([_objects containsObject:newNote]) {
        // Tell the user this note already exists
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Already exists" message: @"This note already exist, sorry" delegate: nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
    } else {
        // The note doesn't exist, add it
        // Initialize objects
        if (!_objects) {
            _objects = [[NSMutableArray alloc] init];
        }

        // Add
        [_objects insertObject:newNote atIndex:0];
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
        [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

        // Save the new _objects
        [self saveObjects];
    }
}
}

-(void)saveObjects {
// Create the key
NSString *partOfKey = @"-notes";

    // Save the new objects
NSString *notesKey = [NSString stringWithFormat:@"%@%@", openSubject, partOfKey];
[[NSUserDefaults standardUserDefaults] setObject:_objects forKey:notesKey];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
// Return the number of sections.
return 1;
}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}

NSString *object = _objects[indexPath.row];
cell.textLabel.text = [object description];
return cell;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
    [_objects removeObjectAtIndex:indexPath.row];
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
调试器输出:

2014-05-21 20:43:14.564 myApp[263:60b]视图显示 2014-05-21 20:43:15.093 myApp[263:60b]视图显示 2014-05-21 20:43:29.741 myApp[263:60b]*由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:'-[\uu NSCFArray insertObject:atIndex:::]:将突变方法发送到不可变对象' *第一次抛出调用堆栈: (0x2d95bfd3 0x38440ccf 0x2d95bf15 0x2d8cfa93 0xf6e29 0x3038eb29 0x3038e7fb 0x3029605f 0x30348377 0x301f76f5 0x3017055b 0x2d9272a5 0x2d924c49 0x2d924f8b 0x2d88fcf3 0x32789663 0x301db16d 0xf9411 0x3894dab7) libc++abi.dylib:以NSException类型的未捕获异常终止 (lldb)

请帮我解决这个问题,我不知道


提前感谢

当您从NSUserDefault获取数组时,它将返回一个不可变数组。您需要获取mutableCopy或执行以下操作_objects=[NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults]objectForKey:notesKey]]


先生,你让我高兴极了!谢谢-我不知道这让我有多烦恼:)