View Full Version : Concatenating Obj C Variables...

Sep 15, 2008, 07:43 AM
hi all,
just after a bit of beginner advice really, i am trying to concatenate 2 NSString variables i have but to be honest its not working out like i hoped...

any help would be great. i have seen loads of examples of appending literal strings to new variables etc but none of it seems to work when i try it.

i have a loop that goes around 0 - 30 and during that loop it randomly picks an array item, this gets stored into an NSString, what i want todo is append all the items together in the array with maybe a space inbetween.

i have tried with no success both items below
[mystring appendString:[myarray objectAtIndex:loopcount]];
mystring = [mystring appendString:[myarray objectAtIndex:loopcount]];

can someone please point me in the direction of what i need to sort this out?

i come from a delphi background and it was as easy as

mystr := mystr + array[i]



Sep 15, 2008, 07:55 AM
NSString is an immutable class: you cannot alter the value once it is created. So if you have an NSString and you are trying to append another string the correct method is stringByAppendingString: (http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html). This returns a new string.


NSString *result = @"";
for (NSString *string in myArray)
result = [result stringByAppendingString:string];

Obviously this creates a load of autoreleased variables (and result is autoreleased at the end). If we wish to avoid that we'd use a NSMutableString (http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSMutableString_Class/Reference/Reference.html) which supports in place appending:

NSMutableString *result = @"";
for (NSString *string in myArray)
[result appendString:string];

I'm sure this is all covered clearly and understandably in the documentation. In particular the String Programming Guide (http://developer.apple.com/documentation/Cocoa/Conceptual/Strings/introStrings.html).

Sep 15, 2008, 08:21 AM
strangely that doesn't work? what am i doing wrong.....

here is the procedure from my program nothing fancy of clever, just want it to generate random strings 10 chars in length

- (IBAction)Generate:(id)sender
int i;
int generated;
NSMutableString *test = @"";

generated = (random() % 26);

[ShowData setIntValue:generated];
NSArray *myarray;
myarray = [NSArray arrayWithObjects :@"a", @"b", @"c", @"d", @"e", @"f", @"g", @"h", @"i", @"j", @"j", @"k", @"l", @"m", @"n", @"o", @"p", @"q", @"r", @"s", @"t", @"u", @"v", @"w", @"x", @"y", @"z",nil];

for ( i=0; i<10; i++) {
generated = (random() % 27);
[test appendString:[myarray objectAtIndex:generated]];


Sep 15, 2008, 08:28 AM
strangely that doesn't work? what am i doing wrong.....

What doesn't work? That is simply not descriptive enough to allow anyone to help you. Does it not compile? If not what are the compiler errors (and why are you not using them to fix it: they tell you everything you need to know)? Does it compile but crash? If so where (use the debugger)? If it does compile and doesn't crash what does it do? How does this differ from what you expect?

Sep 15, 2008, 08:34 AM
checking the output of NSlog when the app is running the app does not produce the concatenated string i was expecting from the loop. using the code you suggested, or the code i have tried so far.

i have tried a manor of ways so far and none of them seem to work.

there are no compile time errors but i do get a warning that makes no sense to me since this is only my fifth day of programming in this language...

at the top of the procedure where it states - (IBaction) Generate:(id)sender

i get a warning triangle stating "Initialization from incompatible pointer type."

Sep 15, 2008, 08:37 AM
i get a warning triangle stating "Initialization from incompatible pointer type."

Normally means you are trying to set a variable to the wrong object type. I suspect it's the

NSMutableString *test = @"";

line. I typed my examples on my work PC so they are unchecked. I rather assumed you'd read the documentation I pointed you at rather than just copy/pasting.

Try changing that line to

NSMutableString *test = [[NSMutableString alloc] init];

Note that it will then be your responsibility to release this string or you will leak memory.

Sep 15, 2008, 10:17 AM
After a few minor changes in the code it compiles and runs just fine now:

NSMutableString *test = [[NSMutableString alloc] init];

NSArray *myArray = [[NSArray alloc] initWithObjects:@"a", @"b", @"c", @"d", @"e", @"f", @"g",
@"h", @"i", @"j", @"j", @"k", @"l", @"m",
@"n", @"o", @"p", @"q", @"r", @"s", @"t",
@"u", @"v", @"w", @"x", @"y", @"z", nil];

for ( i=0; i<10; i++)
[test appendString:[myArray objectAtIndex:random() % 27]];
printf ("Complete string: %s\n", [test UTF8String]);
[test release];
[myArray release];

At first, I was about to change 27 to 26, but you have two letters 'j' so I left it as it was.

Sep 16, 2008, 10:06 AM
thanks for the reply lads both suggestions worked for me, the program is now working great, much appreciated. (will try to post more detail next time and take some more time over the documentation first)