What I am doing is
//ClassB.h
@property (strong, nonatomic) NSString *name;
and
//ClassA.h
@interface ClassA : NSObject
+(ClassA*)methodA:(NSData*)data;
-(id)initWithData:(NSData*)data;
@property (nonatomic, strong) NSMutableArray *arr;
@property (nonatomic, strong) RXMLElement *rxmlRoot;
@end
//ClassA.m
@implementation ClassA
@synthesize arr;
@synthesize rxmlRoot;
+(ClassA*)methodA:(NSData*)data {
return [[ClassA alloc] initWithData:data];
}
-(id)initWithData:(NSData*)data {
self = [super init];
if (self) {
arr = [NSMutableArray array];
rxmlRoot = [RXMLElement elementFromXMLData:data];
/*****edit : just been added to make codes clear*****/
NSString *node = @"players.player";
[rxmlRoot iterate:node with:^(RXMLElement *e){
ClassB *classB = [[[ClassB alloc] init] autorelease];
[classB setName: [e attribute:@"name"]];
// adding ClassB into arr
[arr addObject:classB];
}];
}
return self;
}
@end
So now I am having ClassA object whose arr contains ClassB
Question : later on, when I try to access an particular property of ClassB like
((ClassB*)[classA.arr objectAtIndex:0]).name
and I am getting EXC_BAD_ACCESS at above line..
Please advice me on this issue and how to correct the error. Any comments are welcomed here
Thanks
This line
makes no sense. Is your intention to put an instance of ClassB into that array, or the class itself (i.e.
[ClassB class])? Presumably you must intend to put an instance of ClassB in there, otherwise trying to access its properties later on (e.g.firstName) would make no sense. Also, does your ClassB even have afirstNameproperty, because the piece of ClassB’s interface that you show us only mentions anameproperty.Update:
Since you are using manual memory management, you need to retain the objects (arr, rxmlRoot) you create in your initializer using convenience constructors, which return autoreleased objects. For example, the code should be