iPhone SDK:NSMutableArray计数导致EXC_BAD_ACCESS

spr*_*ain 2 iphone exc-bad-access nsmutablearray

这真的在扭曲我的想法...我正在尝试访问我在viewDidLoad中定义的IBAction中的NSMutableArray.不幸的是,我一直在获得EXC_BAD_ACCESS.

我对这一切都很陌生,所以我真的很感激我对自己做错了什么.

下面找到相应的代码摘录.

CounterViewController.h:

@interface CounterViewController : UIViewController{
 NSMutableArray *countHistoryArray;
}
@property(nonatomic, retain) NSMutableArray *countHistoryArray;
Run Code Online (Sandbox Code Playgroud)

CounterViewController.m:

@implementation CounterViewController
@synthesize countHistoryArray;

- (void)viewDidLoad {
    [super viewDidLoad];

 //Fill array with some dummy data
 self.countHistoryArray = [[NSMutableArray alloc] init];
 NSDate *now = [[[NSDate alloc] init] autorelease];
 CurrentCount *historicCount = [[[CurrentCount alloc]
         initWithCount:[NSNumber numberWithInteger:22]
         description:@"Testcount"
         dateAndTime:now] autorelease];

 [self.countHistoryArray addObject: historicCount];

 //Do some logging - everything is working fine here!
 NSLog(@"%@", [self.countHistoryArray description]); 

}


//Later on we click on a button and want to use the array
- (IBAction)doSomeStuff {  
    //Let's look at the array again - and now it crashes with EXC_BAD_ACCESS
 NSLog(@"%@", [self.countHistoryArray description]);
}
Run Code Online (Sandbox Code Playgroud)

非常感谢!
曼努埃尔


编辑 @jamapag要求的附加代码

CurrentCount.h

#import <Foundation/Foundation.h>


@interface CurrentCount : NSObject {
    NSNumber *counterLevel;
    NSString *description;
    NSDate *dateAndTime;
}

- (id)initWithCount:(NSNumber *)newCounterLevel description:(NSString *)newDescription dateAndTime:(NSDate *)newDateAndTime;

@property(nonatomic, copy) NSNumber *counterLevel;
@property(nonatomic, copy) NSString *description;
@property(nonatomic, copy) NSDate *dateAndTime;

@end
Run Code Online (Sandbox Code Playgroud)

CurrentCount.m

#import "CurrentCount.h"


@implementation CurrentCount
@synthesize counterLevel;
@synthesize description;
@synthesize dateAndTime;

- (id)initWithCount:(NSNumber *)newCounterLevel description:(NSString *)newDescription dateAndTime:(NSDate *)newDateAndTime{
    self = [super init];
    if(nil != self){
        self.counterLevel = newCounterLevel;
        self.description  = newDescription;
        self.dateAndTime  = newDateAndTime;
    }
    return self;
}


-(void) dealloc{
    self.counterLevel = nil;
    self.description  = nil;
    self.dateAndTime  = nil;
    [super dealloc];
}

@end
Run Code Online (Sandbox Code Playgroud)

Nic*_*rge 10

你确定你的代码实际上是这样的吗?

- (IBAction)doSomeStuff {  
    //Let's look at the array again - and now it crashes with EXC_BAD_ACCESS
    NSLog(@"%@", [self.countHistoryArray description]);
}
Run Code Online (Sandbox Code Playgroud)

您的问题标题说"NSMutableArray count导致EXC_BAD_ACCESS" - 如果该行代码实际上说NSLog(@"%@", [self.countHistoryArray count]);,您几乎肯定会遇到崩溃,因为它NSLog会尝试将原始类型(返回的类型-[NSArray count])视为对象.为了使用-[NSArray count]NSLog,用%u的,而不是%@:

- (IBAction)doSomeStuff {  
    // This time it should work!
    NSLog(@"Array Count = %u", [self.countHistoryArray count]);
}
Run Code Online (Sandbox Code Playgroud)