访问NSMutableArray Count ios时应用程序崩溃

时间:2014-04-01 09:39:26

标签: ios iphone objective-c

我遇到了一个奇怪的问题,我无法使用崩溃我的应用程序的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

}
  

错误:    - [LSClaimReport count]:发送到实例0xa54afc0的无法识别的选择器   2014-04-01 14:56:29.022 LossAdjusting [6956:70b] * 由于未捕获的异常终止应用程序' NSInvalidArgumentException',原因:' - [LSClaimReport count] :无法识别的选择器发送到实例0xa54afc0'

我在这里错过了什么。看起来很傻。请指导。 感谢

4 个答案:

答案 0 :(得分:2)

简而言之,您正在将LSClaimReport的实例视为NSMutableArray实例。

结束。

编辑好的,抛开一边,你对实例变量和局部变量感到困惑,并混淆了你的一个实例变量的类型。

ViewDidLoad中(如果不正确,那么如果它被逐字复制,那么它甚至不会被调用),你引用一个本地版本的claimReports,它被创建然后被丢弃:

-(void)ViewDidLoad
{
    NSMutableArray *claimReports = [[NSMutableArray alloc] init];
    [claimReports addObjectsFromArray:[[LADataModelController getSingleton] getClaimReportsOrderedByIncidentDate] ];
}

稍后您将引用实例变量version:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return claimReports.count; //crashes here
}

这显然是LSClaimReport个实例,而不是NSMutableArray

所以看起来像:

  1. 您在@interface
  2. 中声明了错误的类型
  3. 您没有正确初始化它(您已修复该位)。

答案 1 :(得分:0)

请删除本地Arraydeclaration:

NSMutableArray * claimReports = [[NSMutableArray alloc] init];

改为使用:

claimReports = [[NSMutableArray alloc] init];

如果还没有类变量,请将其添加到@interface-declaration:

@interface LAMasterViewController ()
{
    NSMutableArray * claimReports
}
@end

@implementation LAMasterViewController

答案 2 :(得分:0)

将此内容写入您的.h文件

   NSMutableArray * claimReports;

和你的.m

    -(void)viewDidLoad{
              claimReports = [[NSMutableArray alloc] init];
             [claimReports addObjectsFromArray:[[LADataModelController getSingleton] getClaimReportsOrderedByIncidentDate] ];
              }

答案 3 :(得分:0)

您的代码几乎没有问题,清理它们有助于解决问题,主要问题'-[LSClaimReport count]: unrecognized selector sent to instance 0xa54afc0'是因为您在count的实例上调用了LSClaimReport不应该发生。您的代码似乎认为claimReportsLSClaimReport的实例,而不是NSMutableArray的实例。

至于您的代码,我建议您更改为(请参阅标记为问题的代码中的注释)

@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