1

我是 IOS 编程和构建核心数据概念的新手。我遵循苹果文档中的指南。我有一个表视图控制器和一个可变数组 StocksArray 作为其属性之一。我有另一个视图,DetailedController,它用于获取核心数据模型实体属性的用户输入。我将 NSManagedObject 传递给DetailedController 对象,并使用委托来更新可变数组StocksArray。在编译时,出现错误---由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“ * -[NSMutableArray insertObject:atIndex:]:尝试在 0 处插入 nil 对象”。如果有人可以帮助我,我将不胜感激。

#import"StockEntity.h"
@protocol DetailedControllerdelegate <NSObject>

-(void)addstockinarray:(StockEntity*)anObject;

@end



@interface DetailedController : UIViewController {

IBOutlet UITextField* stocksymboltextfield;
IBOutlet UITextField* stocknametextfield;

//NSManagedObjectContext* context2;

StockEntity* asinglestock;

}
@property (nonatomic, retain) IBOutlet UITextField* stocksymboltextfield;
@property (nonatomic, retain) IBOutlet UITextField* stocknametextfield;
//@property(nonatomic, retain) NSManagedObjectContext* context2;
@property(nonatomic, retain) StockEntity* asinglestock;
@property (nonatomic, assign) id <DetailedControllerdelegate> delegate;

@implementation DetailedController

@synthesize stocksymboltextfield, stocknametextfield,asinglestock, delegate;//context2;

-(void)onDone:(id)sender{
[asinglestock setStockName:self.stocknametextfield.text];
[asinglestock setStockSymbol:self.stocksymboltextfield.text];

NSLog(@"lets s see: %@", [asinglestock StockName]);
NSError *error = nil;

if (![[asinglestock managedObjectContext] save:&error] ) {
    //handle the error
}


if ([delegate respondsToSelector:@selector(addstockinarray:)]) {

    NSLog(@"before delegate");

    [delegate addstockinarray:asinglestock];

    NSLog(@"am in delegate");

}


[self.navigationController popViewControllerAnimated:YES];

}

这是表格视图的实现

@implementation maintable

@synthesize stocksArray, managedObjectContext, addButton;

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

    self.stocksArray =[[NSMutableArray alloc]init];
}
return self;
}

- (void)viewDidLoad {
[super viewDidLoad];

//self.stocksArray =[[NSMutableArray alloc]init];
self.title = @"Stocks";

// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
self.navigationItem.leftBarButtonItem = self.editButtonItem;

addButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd 
                                                         target:self 
                                                         action:@selector(addStock)];
//addButton.enabled = NO;
self.navigationItem.rightBarButtonItem = addButton;
}


-(void) addStock{

DetailedController *detail = [[DetailedController alloc]initWithNibName:NSStringFromClass([DetailedController class]) bundle:[NSBundle mainBundle]];
//detail.context2 = managedObjectContext;
StockEntity *stock = (StockEntity*)[NSEntityDescription insertNewObjectForEntityForName:@"StockEntity"
                                                                 inManagedObjectContext:managedObjectContext];

detail.asinglestock = stock;
detail.delegate = self;

//[stocksArray insertObject:stock atIndex:0];


[self.navigationController pushViewController:detail animated:YES];

[detail release];



}

-(void)addstockinarray:(StockEntity*)anObject{

if (anObject = NULL) {
    NSLog(@"YESSS");
}
[self.stocksArray addObject:anObject];

NSLog(@"LETTTTTT: %i", [self.stocksArray count] );

}




- (void)viewWillAppear:(BOOL)animated {

[self.tableView reloadData];
[super viewWillAppear:animated];
}
4

1 回答 1

1

试试这个来处理崩溃,

- (void)addstockinarray:(StockEntity*)anObject{

  if (anObject) {
    [self.stocksArray addObject:anObject];
  } else {
    NSLog(@"Object is nil, do the error handling here");
  }

  NSLog(@"LETTTTTT: %i", [self.stocksArray count] );
}

您的问题是您正在尝试向数组插入 nil 值。你应该检查为什么anObject是 nil 并在那里修复它。如果我是正确的,asinglestock不知何故为零,而您正试图插入它。

于 2012-11-29T06:05:25.707 回答