繁体   English   中英

访问NSMutableArray Count iOS时应用崩溃

[英]App crashes when accessing NSMutableArray Count ios

我陷入了一个奇怪的问题,即我无法使用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计数]:无法识别的选择器发送到实例0xa54afc0 2014-04-01 14:56:29.022 LossAdjusting [6956:70b] *由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'-[LSClaimReport计数]:无法识别的选择器已发送到实例0xa54afc0'

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

长话短说,您正在将LSClaimReport实例视为NSMutableArray实例。

结束。

编辑好,除了活动面板,您对实例变量和局部变量感到困惑,并混淆了实例变量之一的类型。

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. 您没有正确初始化它(您已经修复了该位)。

请删除本地Arraydeclaration:

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

改为使用:

claimReports = [[NSMutableArray alloc] init];

如果还没有Class变量,请将其添加到您的@ interface-declaration中:

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

@implementation LAMasterViewController

将其写到您的.h文件中

   NSMutableArray * claimReports;

并在您的.m中

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

您的代码中有几处错误,清理它们将有助于解决问题,主要问题是'-[LSClaimReport count]: unrecognized selector sent to instance 0xa54afc0'是因为您正在调用LSClaimReport实例上的countLSClaimReport这显然是不应该的。不会发生。 您的代码似乎认为claimReports是一个实例LSClaimReport ,而不是一个实例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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM