传递核心数据中的对象

时间:2012-11-29 05:23:22

标签: objective-c ios xcode core-data nsmanagedobject

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

#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];
}

1 个答案:

答案 0 :(得分: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为零并在那里修复它。如果我是正确的,asinglestock在某种程度上是零,你试图插入它。