2011-01-13 8 views
2

안녕하세요이 문제는 선언과 초기화 프로그램목적 C는

[[accidtest initWithString:@"0293749372920383"]freeWhenDone:NO]; 
[[accnumber alloc] init]; 
[[accprefix alloc] init]; 

static NSString* accidtest; 
static NSString* accnumber; 
static NSString* accprefix; 

나중에는 NSString 3 개 변수를 그리고 XML 에서 그들에게 값을 준 한 한 like

accnumber = string; 
accprefix = string; 

그러나 마지막으로 나는 그들을 사용하고 싶다. nly 인수가 NSCFString으로 바뀌고 다른 값을 저장하면 범위를 벗어납니다. 어떤 도움

답변

2

뭔가를해야이 더 정확 :

/* these initialize to nil */ 
static NSString* accidtest; 
static NSString* accnumber; 
static NSString* accprefix; 

/* ... */ 

/* ??? freeWhenDone */ 
assert(nil == accidtest); /* make sure we aren't creating twice, leaking the previous assignment */ 
accidtest = [[NSString alloc] initWithString:@"0293749372920383"]; 

assert(nil == accnumber); /* make sure we aren't creating twice, leaking the previous assignment */ 
accnumber = [[NSString alloc] initWithString:string]; 

assert(nil == accprefix); /* make sure we aren't creating twice, leaking the previous assignment */ 
accprefix = [[NSString alloc] initWithString:string]; 
1

에 대한

감사는 당신은 초기화되지 않은 변수에 -alloc를 호출하는 것을 시도하고있다. 이것은 아무 의미가 없으며, 컴파일러는 여러분에게 그것을 외쳐야합니다. 대신 당신이

accidtest = [[NSString alloc] initWithString:@"0293749372920383"]; 
accnumber = [[NSString alloc] init]; // what's the point of this? Equivalent to @""