views:

108

answers:

1

Hi, I have following code which is called numerous times:

NSString *Final =  [ NSString stringWithFormat:@"%@%@%@%@.%@%@", str1, str2, str3, str4, str5, str6];

Every once a while Final string is set to nil, while str1 ... to str6 are perfectly valid string (I have verified in debugger)!

I had this code originally, but every once a while Final was also an invalid pointer

NSString *Final =  [ [NSString alloc] initWithFormat:@"%@%@%@%@.%@%@", str1, str2, str3, str4, str5, str6];
...
[Final release];

I am absolutely puzzled. What could this be?

Basically I am collecting values from various edit boxes, form a string from these values and eventually convert it to the number.

Thanks!

A: 

this is a clumsy alternative. If your line does not work, try this:

NSString * finalstr = str1;
finalstr = [finalstr stringByAppendingString:str2];
finalstr = [finalstr stringByAppendingString:str3];
finalstr = [finalstr stringByAppendingString:str4];
finalstr = [finalstr stringByAppendingString:str5];
finalstr = [finalstr stringByAppendingString:str6];

by the way, i used to avoid those commonly used reserved words to be my variable names (though final is not a reserved word by objective-c).

Shivan Raptor
Downvote: non-equivalent workaround. Plus: bug avoiding instead of finding is a bad idea.
Nikolai Ruhe
Shivan Raptor
I have ended up re-writing code using plain C pointers (which I have used for last 15+ years). I have char string[7]. I then fill each char [i] with the respective digit, put the NULL to last value and create and then call decimalNumberFromString (or atof or atoi)
leon
is this really ended up with such complexity? i got no problem when combining strings.
Shivan Raptor