2011-07-04 5 views
3

아래에 설명 된 하나의 포인터 상황에 대해 혼란 스럽습니다. 클래스에서포인터 혼란

@property(nonatomic,retain)NSMutableArray *words; 

//creating a pointer to words from classB 
self.words = [classB words]; 

지금은 클래스 A의 단어 배열에 새로운 단어를 추가하는 경우 클래스 B로 진행되는

@property(nonatomic,retain)NSMutableArray *words; 

, 왜 나는 수업 시간에 단어 배열에서 그 단어가 표시되지 않습니다 비? 클래스 B의 단어 배열이 클래스 A 단어의 포인터라고 생각 했습니까?

+0

둘 다 다른 클래스에 속합니다. 그러면 어떻게 가능할까요? –

+0

@Cyprian 'classB'의'words' 속성이 과제를 수행 할 때 인스턴스화됩니까? –

+1

작동해야합니다. 코드에 배열이 다른 원인이되는 다른 것이 있습니다. –

답변

1

당신 중 일부는 여기에서 일해야한다고 말했듯이 그렇습니다. 내 코드에 몇 가지 오류가있었습니다.

#import "PointersAppDelegate.h" 

#import "PointersViewController.h" 

#import "ClassA.h" 
#import "ClassB.h" 

@implementation PointersAppDelegate 

@synthesize window=_window; 

@synthesize viewController=_viewController; 

@synthesize classA, classB; 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    classA = [[ClassA alloc] init]; 
    classB = [[ClassB alloc] init]; 

    self.window.rootViewController = self.viewController; 
    [self.window makeKeyAndVisible]; 

    //Add new word to words in classA 
    [[classA words] addObject:@"two"]; 

    //Print words in classB 
    [classB print]; 

    return YES; 
} 

- (void)dealloc 
{ 
    [_window release]; 
    [_viewController release]; 

    [classA release]; 
    [classB release]; 

    [super dealloc]; 
} 

@end 

// 클래스 A

// ClassA.h 

@interface ClassA : NSObject { 
    NSMutableArray *words; 
} 
@property(nonatomic,retain)NSMutableArray *words; 

@end 


// ClassA.m 

#import "ClassA.h" 

@implementation ClassA 

@synthesize words; 


- (id)init{ 
    self = [super init]; 
    if (self) { 
     words = [[NSMutableArray alloc] initWithObjects:@"one", nil]; 
    } 
    return self; 
} 


- (void)dealloc { 
    [words release]; 
    [super dealloc]; 
} 
@end 

ClassB가 그 결과

// ClassB.h 

#import <Foundation/Foundation.h> 


@interface ClassB : NSObject { 
    NSMutableArray *words; 
} 
@property(nonatomic,retain)NSMutableArray *words; 

-(void)print; 

@end 


// ClassB.m 
#import "ClassB.h" 
#import "PointersAppDelegate.h" 

@implementation ClassB 

@synthesize words; 

- (id)init{ 
    self = [super init]; 
    if (self) { 
     self.words = [[(PointersAppDelegate*)[[UIApplication sharedApplication] delegate] classA] words]; 
    } 
    return self; 
} 

-(void)print{ 
    for(int i=0;i<[words count];i++) 
     NSLog(@"%@", [words objectAtIndex:i]); 
} 

- (void)dealloc { 
    [words release]; 

    [super dealloc]; 
} 
@end 

입니다

:

그러나 이것은 어떻게해야임을 증명하기 위해 약간의 예제 코드를주고 싶어
2011-07-04 12:38:33.759 Pointers[20059:707] one 
2011-07-04 12:38:33.767 Pointers[20059:707] two 
1

참조 두 개체 중 하나를 코드에서 변경할 때까지 변경 내용은 기본적으로 두 배열에 모두 반영되어야합니다. 이 두 클래스의 단어 배열이 다른 개체를 가리 키도록 할 것

self.words = someOtherArray; 

//creating a pointer to words from classB 
self.words = [classB words]; 

그리고 당신의 클래스 A 또는 클래스 B에서 어떤 곳.

+0

저는 retain이 객체의 소유권을 선언하고 객체의 복사본을 생성하지 않는다고 생각했습니다. – Cyprian

+0

@Cyprian, 예. 죄송합니다. 당신 말이 맞아요. 다른 두 어레에 배열을 지정하지 않았는지 확인하십시오. - EmptyStack – EmptyStack

+0

실제로 Bavarious가 말했듯이 아이디어는 정확합니다. 코드에 오류가 있습니다. – Cyprian

1

실행하려고하는 코드는 무엇입니까? 그렇다면 단어이 [classB words]와 동일하므로 클래스 B 코드에 오한이있는 것 같습니다. (B 클래스 : self.words = [classB words]). 다음과 같은 명령어가있을 수 있습니다 : self.words = [classA words] .... (클래스 A가 클래스 A의 객체라고 가정) 문제를 해결해야합니다.