views:

928

answers:

3

I have this:

if (soapResults != nil) {
 soapResults = [soapResults stringByAppendingString:@"\n"];
 }

But I get a warning:

Assignment from distinct Objective-C type on build.

When I run it on device I get:

Program received signal: "EXC_BAD_ACCESS".

from gdb.

Any ideas how to append a newline to an NSString without getting a warning or error?

Any help appreciated // :)

+2  A: 

An alternative is: [NSString stringWithFormat: @"%@\n", soapResults] even if the logic is the same!

However, even in the case of stringByAppendingString, it returns a new string but it doesn't change the target, so you have to assign the new string to a new instance of the class NSString.

BitDrink
That worked, thanks // :)
Spanky
Don't use format strings unless you actually need to. It's expensive to parse them when you could just use -stringByAppendingString:
NSResponder
A: 

You are likely to have memory management issues, soapResults is probably a stray pointer to an object that has been deallocated. Hard to say without seeing more code, but checking where the object has been originally allocated is a good start.

For example if it has been allocated via a [NSString stringWith... ] call then it is autoreleased by default, i.e. it will be released at the end of the run loop and you have to retain it if you want to hold on to it.

duncanwilcox
A: 

The object you get back from -stringByAppendingString: is autoreleased. You have to retain it if you want it to persist. If you don't retain it, you'll get a memory exception the next time you try to access it.

An easy way to make sure this happens is to declare "soapResults" as a property, and synthesize accessors for it.

NSResponder