Concatenating Obj C Variables...

Discussion in 'Mac Programming' started by kristiaand, Sep 15, 2008.

  1. macrumors member

    Joined:
    Dec 5, 2007
    #1
    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
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    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.
     
  3. thread starter macrumors member

    Joined:
    Dec 5, 2007
    #3
    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);
    	}
    
    }
     
  4. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    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?
     
  5. thread starter macrumors member

    Joined:
    Dec 5, 2007
    #5
    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."
     
  6. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #6
    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.
     
  7. macrumors regular

    Joined:
    Sep 11, 2008
    #7
    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.
     
  8. thread starter macrumors member

    Joined:
    Dec 5, 2007
    #8
    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)
     

Share This Page