tags:

views:

87

answers:

2

Hello Everyone,

The following code is causing me some problems. The third line causes a program crash... it doesn't happen the first time I step through but somehow later on in the program. If I comment out that third line, the program runs smoothly.

NSString *myRequestString = @"text";
int i = 1;
myRequestString = [myRequestString stringByAppendingString:[NSString stringWithFormat: @"t=%d", i]];

That code causes this exception:

* -[CFString release]: message sent to deallocated instance 0xb4c43fe0

On a side note, can anyone tell me how to concatenate strings in objective-c like any other normal language... I can't believe that there is no concatenation operator.

Any and all help is greatly appreciated.

A: 

It sounds like you're releasing myRequestString at some point without retaining it, which is not correct according to the memory management rules.

And no, there's no concatenation operator. There isn't one in C either.

Chuck
A: 

You need a NSMutableString to do that:

NSMutableString *myRequestString = [[NSMutableString alloc] initWithCapacity:20];
[myRequestString appendString:@"text"];
int i = 1;
myRequestString = [myRequestString appendFormat: @"t=%d", i];

Don't forget: NSString is immutable.

Pablo Santa Cruz
What do you mean "you need NSMutableString to do that"? NSString works just fine the way he's doing it — it won't do an in-place append, but it will work just fine. He's just not managing his memory correctly at some point. If you use an autoreleased NSMutableString, it will blow up in the same way.
Chuck