[英]App crashes when accessing NSMutableArray Count ios
I landed into a weird problem where i cannot use array.count that crashes my app. 我陷入了一个奇怪的问题,即我无法使用array.count使我的应用程序崩溃。
@interface LAMasterViewController ()
NSMutableArray * claimReports
@end
-(void) ViewDidLoad
{
claimReports = [[NSMutableArray alloc] init];
[claimReports addObjectsFromArray:[[LADataModelController getSingleton] getClaimReportsOrderedByIncidentDate] ];
}
-(NSArray *) getClaimReportsOrderedByIncidentDate
{ // it returns one record
NSManagedObjectContext *context = [self managedObjectContext];
NSError *error;
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [ NSEntityDescription entityForName:@"ClaimReport" inManagedObjectContext:context];
NSSortDescriptor *sortByIncidentDate = [[NSSortDescriptor alloc] initWithKey:@"dateOfIncident" ascending:NO];
[request setEntity:entity];
[request setSortDescriptors: [NSArray arrayWithObject: sortByIncidentDate]];
NSArray *array = [context executeFetchRequest:request error:&error];
NSLog(@"Array Count %i" ,array.count);
return array;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return claimReports.count; //crashes here
}
error: -[LSClaimReport count]: unrecognized selector sent to instance 0xa54afc0 2014-04-01 14:56:29.022 LossAdjusting[6956:70b] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[LSClaimReport count]: unrecognized selector sent to instance 0xa54afc0'
错误:-[LSClaimReport计数]:无法识别的选择器发送到实例0xa54afc0 2014-04-01 14:56:29.022 LossAdjusting [6956:70b] *由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'-[LSClaimReport计数]:无法识别的选择器已发送到实例0xa54afc0'
What have i missed here. 我在这里错过了什么。 Seems a silly one.
看起来很傻。 please guide.
请指导。 Thanks
谢谢
Long-story-short, you are treating an instance of LSClaimReport
as if it was an NSMutableArray
instance. 长话短说,您正在将
LSClaimReport
实例视为NSMutableArray
实例。
The end. 结束。
EDIT OK, flippany aside, you are confused about instance variable and local variables and have confused the types of one of your instance variables. 编辑好,除了活动面板,您对实例变量和局部变量感到困惑,并混淆了实例变量之一的类型。
In ViewDidLoad
(case incorrect, so if that's copied verbatim then it won't even be called), you reference a local version of claimReports
which is created and then thrown away: 在
ViewDidLoad
(大小写不正确,因此,如果逐字复制就不会被调用),则引用创建并丢弃的本地版本的claimReports
:
-(void)ViewDidLoad
{
NSMutableArray *claimReports = [[NSMutableArray alloc] init];
[claimReports addObjectsFromArray:[[LADataModelController getSingleton] getClaimReportsOrderedByIncidentDate] ];
}
and later you refer to the instance variable version: 然后您引用实例变量版本:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return claimReports.count; //crashes here
}
Which is clearly a LSClaimReport
instance and not a NSMutableArray
. 显然,这是
LSClaimReport
实例,而不是NSMutableArray
。
So it looks like: 所以看起来像:
@interface
. @interface
声明了错误的类型。 please remove the local Arraydeclaration: 请删除本地Arraydeclaration:
NSMutableArray * claimReports = [[NSMutableArray alloc] init];
instead use: 改为使用:
claimReports = [[NSMutableArray alloc] init];
In case this is no Class variable yet, add it to your @interface-declaration: 如果还没有Class变量,请将其添加到您的@ interface-declaration中:
@interface LAMasterViewController ()
{
NSMutableArray * claimReports
}
@end
@implementation LAMasterViewController
Write this in your .h file 将其写到您的.h文件中
NSMutableArray * claimReports;
and in your .m 并在您的.m中
-(void)viewDidLoad{
claimReports = [[NSMutableArray alloc] init];
[claimReports addObjectsFromArray:[[LADataModelController getSingleton] getClaimReportsOrderedByIncidentDate] ];
}
You have few things wrong with your code which cleaning them up will help resolve the issue, the main issue '-[LSClaimReport count]: unrecognized selector sent to instance 0xa54afc0'
is because you are calling count
on an instance of LSClaimReport
which clearly shouldn't be happening. 您的代码中有几处错误,清理它们将有助于解决问题,主要问题是
'-[LSClaimReport count]: unrecognized selector sent to instance 0xa54afc0'
是因为您正在调用LSClaimReport
实例上的count
, LSClaimReport
这显然是不应该的。不会发生。 Your code seems to think that claimReports
is an instance of LSClaimReport
and not an instance of NSMutableArray
. 您的代码似乎认为
claimReports
是一个实例LSClaimReport
,而不是一个实例NSMutableArray
。
As for your code may I recommend changing to (Please see comments in code marked issue) 至于您的代码,我建议您更改为(请参阅代码中标记为问题的注释)
@interface LAMasterViewController ()
// Clearly you want this as a private property so why would you want to change
// to having this in the .h file which would make it public
// But Issue 1 is here you are missing the semi-colon (`;`) of the end.
@property (nonatomic, strong) NSMutableArray *claimReports;
@end
// Issue two you are missing the `@implementation LAMasterViewController`
@implementation LAMasterViewController
// The synthesize is done automatically so getters/setters/ivar are created automatically
// Issue 3: ViewDidLoad isn't a valid selector so ViewDidLoad will never be called
// It is viewDidLoad
-(void)viewDidLoad
{
[super viewDidLoad]; // Issue 4: you missed the call to super
claimReports = [[NSMutableArray alloc] init];
[claimReports addObjectsFromArray:[[LADataModelController getSingleton] getClaimReportsOrderedByIncidentDate]];
}
-(NSArray *)getClaimReportsOrderedByIncidentDate
{ // it returns one record
NSManagedObjectContext *context = [self managedObjectContext];
NSError *error;
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [ NSEntityDescription entityForName:@"ClaimReport" inManagedObjectContext:context];
NSSortDescriptor *sortByIncidentDate = [[NSSortDescriptor alloc] initWithKey:@"dateOfIncident" ascending:NO];
[request setEntity:entity];
[request setSortDescriptors: [NSArray arrayWithObject: sortByIncidentDate]];
NSArray *array = [context executeFetchRequest:request error:&error];
NSLog(@"Array Count %i" ,array.count);
return array;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return claimReports.count;
}
@end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.