嘗試沿着這些方向行事,我沒有看到您的代碼,因此這可能不是您問題的確切解決方案,但希望它能幫助您瞭解解決問題所需的消息傳遞。
//FirstClass .h file
#import @"SecondClass.h"
@interface FirstClass : NSObject {
NSArray *firstArray;
SecondClass *sec;
}
@property(nonatomic, retain) NSArray *firstArray;
@property(nonatomic, retain) SecondClass *sec;
@end
//Add this to FistClass .m file
@synthesize firstArray, sec;
-(id)init{
if(self == [super init]){
sec = [[SecondClass alloc] init];
firstArray = [[NSArray alloc] initWithArray:sec.secondArray];
}
return self;
}
-(void)dealloc{
[firstArray release];
[super dealloc];
}
//SecondClass .h file
@interface SecondClass : NSObject {
NSMutableArray *secondArray;
}
@property(nonatomic, retain) NSMutableArray *secondArray;
@end
//Add this to SecondClass .m file
@synthesize secondArray;
-(id)init{
if(self == [super init]){
secondArray = [[NSMutableArray alloc] initWithObjects:@"Obj1", @"Obj2", @"Obj3", nil];//etc...
//Maybe add some more objects (this could be in another method?)
[secondArray addObject:@"AnotherObj"];
}
return self;
}
-(void)dealloc{
[secondArray release];
[super dealloc];
}
是否有必要在init方法中編寫這段代碼?因爲我已經有第二數組中的對象來自xml解析 – nehal 2011-04-14 09:44:31
不,只要secondClass中的secondArray在firstClass內執行此行之前被構造:firstArray = [[NSArray alloc] initWithArray:sec.secondArray];如果你沒有做到這一點,那麼secondArray將有一個零值,並且不會包含任何要複製到firstArray中的對象。 – Sabobin 2011-04-14 09:47:20