Simple String Combining

Discussion in 'iOS Programming' started by thisma, Sep 9, 2008.

  1. thisma macrumors member

    Joined:
    Sep 9, 2008
    #1
    I want to put two strings together to form the name of the file passed to a UIImage. I think something like this:

    Code:
    	NSMutableString *tempName = [NSMutableString stringWithString:@"foo"];
    	[tempStemName appendString:@"bar.png"];
    	imageOfStem = [UIImage imageNamed:tempName];
    
    will work.

    My question is: Is there some way of doing this more simply or without so much juggling; this solution seems very bloated.

    I know I can't do this:

    Code:
    	imageOfStem = [UIImage imageNamed:@"foo" + @"bar.png"];
    
    But, something like:

    Code:
    	imageOfStem = [UIImage imageNamed:[@"foo" returnStringWithStringAppended:@"bar.png"];
    
    would be good.

    trick here is @"foo" needs to remain @"foo"

    Sorry to be such a n00b; I'm working on it. I did spend the prerequisite time Googling and found my self more confused afterward.

    Please help,
    -thisma
     
  2. Luke Redpath macrumors 6502a

    Joined:
    Nov 9, 2007
    Location:
    Colchester, UK
  3. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #4
    FYI... use [code] ... [/code] instead of <code> ... </code>
     
  4. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #5
    This will work:

    Code:
    imageOfStem = [UIImage imageNamed:[@"foo" stringByAppendingString:@"bar.png"]];
    imageOfStem = [UIImage imageNamed:[NSString stringWithFormat:@"%@%@" @"foo", @"bar.png"]];
    Working with immutable strings sometimes seems odd vs other languages that have a string concatenation operator.
     
  5. thisma thread starter macrumors member

    Joined:
    Sep 9, 2008
    #6
  6. thisma thread starter macrumors member

    Joined:
    Sep 9, 2008
    #7
    Awesome :D thank you. That's what I was looking for.
     

Share This Page