1

I want to add object to NSMutableArray but after writing following code i am getting benchmarkArray NIL. Where is the mistake help me please

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

My Benchmark class is:

@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
  • Are you sure that coder is properly initialised? Commented Mar 8, 2013 at 10:32
  • he says benchmarkArray is nil, not empty Commented Mar 8, 2013 at 10:33
  • @peko i have already add this line at ViewDidLoad benchmarkArray = [[NSMutableArray]alloc] init]; Commented Mar 8, 2013 at 10:34
  • @Ramy Al Zuhouri yes i am using ARC Commented Mar 8, 2013 at 10:39

2 Answers 2

2

Nothing wrong with your BenchMark Class.

Your problem lies in the class where you have created benchMarkArray.

After making property of it, you need to initialise it in init/awakeFromNib/viewDidLoad method as :

benchMarkArray=[[NSArray alloc]init];

or,

benchmarkArray=[NSArray new];
Sign up to request clarification or add additional context in comments.

1 Comment

- (void)viewDidLoad { [super viewDidLoad]; //set category/benchmark class object self.category = [[Category alloc] init]; self.benchmark = [[Benchmark alloc] init]; } i have already initialized it
1

you forgot to allocate and initialize

benchmarkArray = [[NSMutableArray alloc] init];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.