我想将对象添加到NSMutableArray中,但在编写以下代码后,我得到的结果是benchmarkArray为空。哪里出错了,请帮帮我
    [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发布于 2013-03-08 18:30:03
您忘记了分配和初始化
benchmarkArray = [[NSMutableArray alloc] init];发布于 2013-03-08 18:36:02
您的BenchMark类没有任何问题。
您的问题出在创建benchMarkArray的类中。
对其进行属性处理后,需要在init/awakeFromNib/viewDidLoad方法中将其初始化为:
benchMarkArray=[[NSArray alloc]init];或,
benchmarkArray=[NSArray new];https://stackoverflow.com/questions/15291697
复制相似问题