Not sure why one snippet works while the other fails:
This fails(the app quits) after pressing the button 3 times (this code snipped is executed on a “button press”):
sUser = [NSString stringWithFormat: @"%@ %c", sUser, charcode];//appendstring
Note: that in the header file sUser is defined as NSString and charcode is an int;
This works (this code snippet is executes on a “button press”):
int r;
theString = @"";
for(i = 0; i < iDigits ; i++)
{
r = rand() % 26;
theString = [NSString stringWithFormat: @"%@ %c", theString, r + 65];//appendstring
}
Note: that in the header file theString is defined as NSString;
The reason for the crash is if sUser is defined in the header this means that it belongs to your class and needs to be assigned to a retained string. If you have a property for sUser do this
otherwise
Now there is a better more efficient way for appending strings
NSMutableString.