开发者

Objective C some NSString dont turn to NSCFString

开发者 https://www.devze.com 2023-02-04 04:19 出处:网络
Hi I have a problem I have declared and initialized 3 variables NSString static NSString* accidtest; static NSString* accnumber;

Hi I have a problem I have declared and initialized 3 variables NSString

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

later in program

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

And 开发者_运维知识库I gave them a value from xml like that

accnumber = string;
accprefix = string;

but in the end when i want to use them I found the problem that only accnumber turn into NSCFString and save his value other are out of scope.

thank for any help


this is more correct:

/* 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];


You're trying to call -alloc on a non-initialized variable. This doesn't make any sense, and the compiler should be yelling at you for it. Instead you should have something like

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

精彩评论

暂无评论...
验证码 换一张
取 消