gpt4 book ai didi

objective-c - 在核心数据中传递对象

转载 作者:行者123 更新时间:2023-11-29 13:24:03 25 4
gpt4 key购买 nike

<分区>

我不熟悉 IOS 编程和构建我的核心数据概念。我遵循苹果文档中的指南。我有一个 TableView Controller 和一个可变数组 StocksArray 作为其属性之一。我有另一个 View DetailedController,它用于为核心数据模型的实体属性获取用户输入。我将 NSManagedObject 传递到 DetailedController 对象并使用委托(delegate)来更新可变数组 StocksArray。编译时出现错误——由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“* -[NSMutableArray insertObject:atIndex:]: attempt to insert nil object at 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];

}

这是 TableView 的实现

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com