![](/img/trans.png)
[英]iOS app crashing with -[NSCFString count]: when trying to get count of NSMutableArray if count is only 1
[英]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
。
所以看起來像:
@interface
聲明了錯誤的類型。 請刪除本地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
實例上的count
, LSClaimReport
這顯然是不應該的。不會發生。 您的代碼似乎認為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.