I am new to Objective-C however I am slowly making progress. All I would like to do is append a new string to a current string. I need to do this for my NSURLConnection so that I can put all the data into one string. I currently have something like this, however it does work and I don’t fully understand why and what I need to do to fix it.
NSString *temp = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
html = [htmlTemp stringByAppendingString:temp];
htmlTemp = html;
Thanks For any help I am sure its something to do with the memory however I don’t fully understand that either.
StringByAppendingStringis a convenience method that does not start with “new” or contains “alloc” or “copy”, the returned string will beautoreleasedmost likely in the next run cycle, which is why when you try to read it again from html, it has already been released. To fix this you can send acopymessage like this:I would recommend making htmlTemp a property like this:
This way when you assign a new value to it, using the setter, the old value will get released, before pointing to the new value that way you can do: