2011-04-14 1 views
0

안녕하세요
한 배열의 요소를 다른 클래스에있는 다른 배열에 복사하고 싶습니다.
나는 여러 가지 방법을 시도했습니다.객관적인 c에있는 동일한 종류가 아닌 다른 배열에 하나의 배열을 복사하는 방법?

두 배열이 같은 클래스가 아닙니다. 예 secondArray를 들어
I는 다음이

second *sec; //(in first.h) 

같이 second.h 클래스의 객체를 만들어
그것을 합성 할 때 다음 second.h 파일의 파일에 first.h 어레이에 I 이런 식으로 배열을 복사하려고 시도했습니다. sec = [[Second alloc] init];
sec.array = secondarray;
하지만 배열이 null 인 것을 나타내는 두 번째 클래스의 배열에 액세스 할 때

누구든지 이에 대해 알 수 있습니까? 또는 어떤 샘플 코드?

답변

1

이 코드를 따라 뭔가를 시도해보십시오. 문제가 해결 된 정확한 해결책이 아니므로 코드를 보지 못했지만 문제를 해결하는 데 필요한 메시지 전달을 이해하는 데 도움이됩니다.

//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]; 
} 
+0

init 메서드에서이 코드를 작성해야합니까? xml 구문 분석에서 오는 secondarray에 객체가 이미 있기 때문에 – nehal

+0

아니요,이 클래스가 firstClass 내부에서 실행되기 전에 secondClass 내의 secondArray가 생성되는 한 : firstArray = [[NSArray alloc] initWithArray : sec.secondArray]; 이 작업을 수행하지 않으면 secondArray는 nil 값을 가지며 firstArray에 복사 할 개체가 없습니다. – Sabobin

0

그냥 내 머리에서 제안을 외치지 만, sec.array = secondarray을 시도해보십시오.

+0

이 게시물을 문제 해결 방법으로 표시하는 경우 의견을 삭제하는 것이 좋습니다. – Sabobin