Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

kristiaand

macrumors member
Original poster
Dec 5, 2007
63
0
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

HELP PLEASE

thanks
kris
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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:. This returns a new string.

Example:

Code:
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 which supports in place appending:

Code:
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.
 

kristiaand

macrumors member
Original poster
Dec 5, 2007
63
0
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

Code:
- (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]];
		NSLog(test);
	}

}
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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?
 

kristiaand

macrumors member
Original poster
Dec 5, 2007
63
0
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."
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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

Code:
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

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

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

idelovski

macrumors regular
Sep 11, 2008
235
0
After a few minor changes in the code it compiles and runs just fine now:

Code:
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.
 

kristiaand

macrumors member
Original poster
Dec 5, 2007
63
0
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)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.