[英]Terminating app due to uncaught exception 'NSInvalidArgumentException'?
I cannot figure out why as soon as I try to use the [mainArray addObject:] expression my whole program fails! 我不知道为什么一旦尝试使用[mainArray addObject:]表达式,我的整个程序就会失败! If i pass it strings it works just fine, but as soon as i try to pass it a variable it fails, any help? 如果我将其传递给字符串,则可以正常工作,但是一旦我尝试将其传递给变量,它将失败,有什么帮助吗?
#import "AG_ViewController.h"
#import "AG_Storage.h"
#import "AG_AddItemViewController.h"
@interface AG_ViewController ()
@end
@implementation AG_ViewController
- (void)viewDidLoad {
[super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib.
mainArray = [NSMutableArray array];
AG_Storage *item1 = [[AG_Storage alloc] init];
item1.itemName = @"Test";
NSLog(@"Add item1");
NSLog(@"%@",mainArray);
AG_Storage *item2 = [[AG_Storage alloc] init];
item2.itemName = @"Test2";
NSLog(@"Add item2");
[mainArray addObject:item1];
NSLog(@"%@", mainArray);
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [mainArray count]; }
- (UITableViewCell *)tableView:(UITableView *)tableViewer cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableViewer dequeueReusableCellWithIdentifier:@"thisCell"];
cell.textLabel.text = [mainArray objectAtIndex:indexPath.row];
return cell; }
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated. }
@end
I initialized mainArray in the header file, 我在头文件中初始化了mainArray,
@interface AG_ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>{
IBOutlet UITableView *tableView;
NSMutableArray *mainArray;
} }
Log is as follows: 日志如下:
2014-03-18 13:49:26.028 AgendaBk[22575:a0b] Add item1
2014-03-18 13:49:26.030 AgendaBk[22575:a0b] (
)
2014-03-18 13:49:26.030 AgendaBk[22575:a0b] Add item2
2014-03-18 13:49:26.031 AgendaBk[22575:a0b] (
"<AG_Storage: 0x895bdc0>"
)
2014-03-18 13:49:26.036 AgendaBk[22575:a0b] -[AG_Storage length]: unrecognized selector sent to instance 0x895bdc0
2014-03-18 13:49:26.039 AgendaBk[22575:a0b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[AG_Storage length]: unrecognized selector sent to instance
Junk here 垃圾在这里
libc++abi.dylib: terminating with uncaught exception of type NSException
Note (you probably don't actually have it in your code, but anyway): Don't forget NSMutableArray *mainArray = [NSMutableArray array]
注意(您的代码中可能实际上没有它,但是无论如何):不要忘记NSMutableArray *mainArray = [NSMutableArray array]
Try to use [mainArray addObjectsFromArray:@[item1, item2]];
尝试使用[mainArray addObjectsFromArray:@[item1, item2]];
Try this way 试试这个
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSMutableArray *mainArray = [NSMutableArray array];
AG_Storage *item1 = [[AG_Storage alloc] init];
item1.itemName = @"Test";
NSLog(@"Add item1");
NSLog(@"%@",mainArray);
AG_Storage *item2 = [[AG_Storage alloc] init];
item2.itemName = @"Test2";
NSLog(@"Add item2");
[mainArray addObjectsFromArray:[NSArray arrayWithObjects: item1, item2, nil]];
NSLog(@"%@",mainArray);
}
Thanks 谢谢
I think you don't quite understand the difference between an instance variable and a local variable. 我认为您不太了解实例变量和局部变量之间的区别。 If your first code version compiled and you haven't changed your properties, then your first example assigned an NSMutableArray* to an instance variable, where it stays until that view is deallocated, while the second example assigned an NSMutableArray* to a local variable, where ARC will deallocate it as soon as the method returns. 如果您的第一个代码版本已编译且您尚未更改属性,则第一个示例将NSMutableArray *分配给实例变量,该实例变量将一直保留到该视图被释放为止,而第二个示例将NSMutableArray *分配给局部变量,方法返回后,ARC将在其中重新分配它。 item1 and item2 will disappear as well. item1和item2也将消失。
It is highly recommended to start instance variables with an underscore (like _mainArray), to make it more obvious what you are doing. 强烈建议以下划线(例如_mainArray)开头实例变量,以使其在执行操作时更加明显。
Sorry, just noticed that it was someone else changing the instance variable to a local variable. 抱歉,刚刚注意到是有人将实例变量更改为局部变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.