简体   繁体   English

访问NSMutableArray Count iOS时应用崩溃

[英]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: 所以看起来像:

  1. You have declared the wrong type in your @interface . 您在@interface声明了错误的类型。
  2. You are not initializing it correctly (you already fixed that bit). 您没有正确初始化它(您已经修复了该位)。

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实例上的countLSClaimReport这显然是不应该的。不会发生。 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.

相关问题 iOS应用程序崩溃 - [NSCFString count]:当计数仅为1时尝试获取NSMutableArray的计数 - iOS app crashing with -[NSCFString count]: when trying to get count of NSMutableArray if count is only 1 为什么删除AllObjects,分配nil然后初始化NSMutableArray会使iOS App崩溃? - Why RemovingAllObjects, Assigning nil and then initializing NSMutableArray crashes iOS App? iOS中NSMutableArray没有对象时应用崩溃 - App is crashing when NSMutableArray no Objects in iOS 从NSMutableStrings循环填充NSMutableArray时,应用程序崩溃 - App crashes when populating NSMutableArray from loop of NSMutableStrings React Native iOS 8.1应用程序在访问新视图时崩溃 - 仅在不使用调试器时 - React Native iOS 8.1 app crashes when accessing a new view - ONLY when NOT using debugger 在iOS 5上启动时,应用崩溃 - App crashes when starting on iOS 5 从Exchange全局地址列表访问联系人时,iOS App崩溃 - iOS App crashes when accessing a contact from Exchange Global Address List 当应用程序崩溃时,IOS App CoreData会重置 - IOS App CoreData is reset when app crashes iOS 13.4 访问 MPMusicPlayerController.systemMusicPlayer 导致应用程序崩溃 - iOS 13.4 accessing MPMusicPlayerController.systemMusicPlayer crashes the app 应用程序在检查计数NSMutableArray时崩溃 - App crashing on checking count NSMutableArray
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM