2014-04-01 76 views
-1

我登陆到一个奇怪的问题,我无法使用array.count,崩溃我的应用程序。当访问NSMutableArray计数ios时应用程序崩溃

@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'

什么我错过了这里。看起来很愚蠢。请指导。 谢谢

+0

邮政错误的详细信息! – trojanfoe

+0

@trojanfoe请参阅编辑。 – LUI

+0

@LUI只需在完成过程后保留您的数组。 –

回答

2

长篇小说,你正在对待LSClaimReport的实例,就好像它是一个NSMutableArray实例。

结束。

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

ViewDidLoad(情况下不正确,所以如果这是逐字复制,然后它甚至不会被调用),您引用的claimReports本地版本被创建,然后扔掉:

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

,以后你参考实例变量版本:

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

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

所以它看起来像:

  1. 你已经宣布你@interface错误的类型。
  2. 您没有正确初始化它(您已经修复了这一点)。
+0

+1就是这样所有它 –

+1

一个轻浮的答案,当然,但谁知道该代码(和谁想要)发生了什么? – trojanfoe

+0

@trojanfoe我只是想减少代码。它现在可以在新的编辑中使用了。你可以分享任何想法,如果你有这个问题。谢谢。 – LUI

0

请删除本地Arraydeclaration:

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

改用:

claimReports = [[NSMutableArray alloc] init]; 

如果这是没有类变量是,它添加到您@接口声明:

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

@implementation LAMasterViewController 
+0

这是错误的,请参阅'trojanfoe's答案,因为这是完全正确的。 – Popeye

0

写在你的.h文件中

NSMutableArray * claimReports; 

,并在您的.m

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

为什么?是什么使得伊娃在私密性上与众不同? – Popeye

0

你有几件事情错了你的代码,清理起来将有助于解决这个问题,主要问题'-[LSClaimReport count]: unrecognized selector sent to instance 0xa54afc0'是因为你的实例调用countLSClaimReport显然不应该发生。您的代码似乎认为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 
相关问题