2016-07-02 4 views
-2

에 전달할 때 NSMutableDictionary의 새로운 인스턴스를 생성하지만 난 내가 NSMutableDictionary 클래스에서 선언 한 다른 클래스

@interface MyClass0 : NSObject 
{ 

} 

@property (nonatomic, strong) NSMutableDictionary *valuee; 
@end 

및 구현 예를

에 대한 다른 클래스에서의 내용에 대한 액세스를 얻을 인쇄 할 수 없음 내가

@implementation MyClass0 

- (void)viewDidLoad{ 
    [super viewDidLoad]; 

[valuee setObject:@"name" forKey:@"Aryan"]; 

} 

@end 

지금 나는이

@interface MyClass1 : NSObject 
    { 
    } 

    @property (nonatomic, strong) NSMutableDictionary *dict; 

    @end 
에 액세스 할 MyClass1라는 새로운 클래스를 만들려면 어떻게해야합니까

및 구현

@implementation MyClass1 
@synthesize dict; 

- (void)viewDidLoad{ 
    [super viewDidLoad]; 

self.dict = [[NSMutableDictionary alloc] init]; 
MyClass0 *c = [[MyClass0 alloc] init]; 

self.dict = c.valuee; 

    // dict is not nil but the contents inside is nil so it clearly creates a new instance 


} 

@end 
+0

당신은 allocing 당신을 INITING MyClass0하지만보기가 너무 아무것도는 사전에 설정되지지고로드, 안 그래있다. 반면에 귀하의 dict 속성에 대한 사본을 지정했습니다. 그렇습니다. 사본을 작성하고 있습니다. ** 없음 ** - – Remover

+0

나는이 질문을 수정합니다 사고로 복사 넣어 : @Remover –

+0

난 그냥 당신이 감사합니다 :) @Remover –

답변

1

을 초기화되지 않았습니다. 값이 선언 된 속성에 할당되어있는 경우

코드에 가장 가까운 용액은 명시 적으로 초기화가 필요하지 않습니다

MyClass0 *c = [[MyClass0 alloc] init]; 
c.valuee = [[NSMutableDictionary alloc] init]; 

self.dict = c.valuee; 

입니다.

1

이 같은 내용을 당신과 같이 MyClass0에서 클래스 메소드를 생성 할 수 있습니다 때마다이 단순한 NSMutableDictionary의 경우

+ (NSMutableDictionary *) getDict { 
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; 
    [dict setObject:@"name" forKey:@"Aryan"];//did you mean [dict setObject:@"Aryan" forKey:@"name"]? 
    return dict; 
} 

이 접근하기를, 방법을 선언 같은 MyClass0.h 파일에 너무 : + (NSMutableDictionary *) getDict;와 단순히 MyClass1.m 파일에 [MyClass0 getDict];를 호출합니다. 이 같은 내용마다이없는 경우

, 당신은 prepareForSegue 각 뷰 컨트롤러 앞으로 사전을 통과해야합니다 : 선언 당신은 MyClass0valuee의 인스턴스를 생성하는

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
    // Make sure your segue name in storyboard is the same as this next line 
    if ([[segue identifier] isEqualToString:@"MySegue"]) { 

     MyClass1 *mc = [segue destinationViewController]; 
     mc.dict = self.valuee; 
    } 
} 
관련 문제