2012-06-18 3 views
0

다음 코드를 실행하면 실패합니다. 내 .H 파일 다음하는 .m 파일에정적 객체의 멤버를 초기화하는 iOS가 실패합니다.

@interface OutlineManager : NSObject 
    { 
      NSMutableArray* mOutlines; 
    } 

    @property(nonatomic,strong)NSMutableArray* Outlines; 


    +(void)initialize; 

    @end 

    static OutlineManager* outlnManager; 

: 난 (무효) 내가 코멘트를 넣어 경우 섹션에서 응용 프로그램이 충돌을 초기화를 실행하지만 때

#import "OutlineManager.h" 

@implementation OutlineManager 

@synthesize Outlines = mOutlines; 

+(void)initialize 
{ 
    outlnManager = [[[OutlineManager superclass]alloc]init]; 
    if(outlnManager) 
    { 
     outlnManager.Outlines = [[NSMutableArray alloc]init]; //it crashes here 
    } 
    NSLog(@"OUTLINEMANAGER INITIALIZED"); 
} 

@end 

내가 돈 't 왜 그런지 알 겠어. 이처럼 정적 인 객체의 배열을 초기화 할 수 없는가?

나는 아주 기본적인 것 같지만, Obj C/iOS에서 꽤 새로 나온다.

당신의 도움! 감사합니다

, ZOLI

답변

1

당신이 쓴 :

슈퍼 클래스는 NSObject의에하지 OutlineManager에 ALLOC를 호출하는 것을 의미
outlnManager = [[[OutlineManager superclass]alloc]init]; 

. 그냥이이 라인을 교체 :

outlnManager = [[OutlineManager alloc]init]; 

아, 그리고 당신이하는 .m 파일에 정적 변수를 선언해야합니다.

+0

! 그러나 거기에 널 때문에 다른 .m 파일에 outlnManager.Outlines 개체를 추가 할 수 없습니다. 언제 그것이 null이됩니다. ARC가 무료로 제공합니까? –

0

OutlineManager.h :

OBJC_EXPORT OutlineManager* outlnManager; 

및 OutlineManager.m에 추가 :

static OutlineManager* outlnManager; 

대체 작동

OutlineManager* outlnManager = nil; 
+(void)initialize { // you should rename it to an other name.. initialize will be called twice 
    outlnManager = [[OutlineManager alloc]init]; 
    if(outlnManager) { 
     outlnManager.Outlines = [[NSMutableArray alloc]init]; 
    } 
    NSLog(@"OUTLINEMANAGER INITIALIZED"); 
} 
관련 문제