Csabi
Csabi

Reputation: 3043

Objective C some NSString dont turn to NSCFString

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

Upvotes: 2

Views: 1918

Answers (2)

justin
justin

Reputation: 104708

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

Upvotes: 2

Lily Ballard
Lily Ballard

Reputation: 185821

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 @""

Upvotes: 1

Related Questions