1

我想将对象添加到 NSMutableArray 但在编写以下代码后我得到 benchmarkArray NIL。错在哪里请帮帮我

    [benchmark setBenchmarkTitle:self.benchmarkTitle.text];
    [benchmark setBenchmarkDescription:self.benchmarkDescription.text];
    [benchmarkArray addObject:benchmark];

我的基准课程是:

@interface Benchmark : NSObject

@property (nonatomic, retain) NSString *bid;
@property (nonatomic, retain) NSString *benchmarkTitle;
@property (nonatomic, retain) NSString *benchmarkDescription;

-(id)initWithCoder: (NSCoder *)coder;
-(void)encodeWithCoder:(NSCoder *)encoder;

@end

@implementation Benchmark

@synthesize bid;
@synthesize benchmarkTitle;
@synthesize benchmarkDescription;

- (id) initWithCoder:(NSCoder *)coder {

    self = [super init];
    if(self) {
        bid = [coder decodeObjectForKey:@"id"];
        benchmarkTitle = [coder decodeObjectForKey:@"benchmarkTitle"];
        benchmarkDescription = [coder decodeObjectForKey:@"benchmarkDescription"];
    }
    return self;
}

- (void) encodeWithCoder:(NSCoder *)encoder {

    [encoder encodeObject:bid forKey:@"id"];
    [encoder encodeObject:benchmarkTitle forKey:@"benchmarkTitle"];
    [encoder encodeObject:benchmarkDescription forKey:@"benchmarkDescription"];
}

@end
4

2 回答 2

2

您的 BenchMark 课程没有任何问题。

您的问题在于您创建的类benchMarkArray

创建它的属性后,您需要在init/awakeFromNib/viewDidLoad方法中将其初始化为:

benchMarkArray=[[NSArray alloc]init];

或者,

benchmarkArray=[NSArray new];
于 2013-03-08T10:36:02.467 回答
1

你忘了分配和初始化

benchmarkArray = [[NSMutableArray alloc] init];
于 2013-03-08T10:30:03.063 回答