returning strings (novice question)

Discussion in 'Mac Programming' started by Darkroom, May 24, 2008.

  1. Darkroom Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #1
    my coffee doesn't seem to be working just yet:

    Code:
    if (licenseDictionary == nil)
    	return @"Goodbye!";
    	else
    	[B]return @"Hello " [licenseDictionary objectForKey:@"Full Name"];[/B]
    
    i can't seem to remember how to properly merge the bold statement above... "Hello " will always be there, but Full Name will change... it's a syntax error, and i'm not even sure i can change portions of a string dynamically...

    any thoughts?
     
  2. tacoman667 macrumors regular

    Joined:
    Mar 27, 2008
    #2
    Can you add a "+" between the 2 items or my guess would be you need to use a concatinating method in NSString. Something like
    Code:
     return [NSString stringFromObjects:@"Hello ", <Other Obj...>];
    .

    This woul dbe just a guess as I haven't done that yet and I haven't finished my book yet either.
     
  3. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #3
    the comma doesn't work, as only the last object in the sentence is executed... and adding "+" only seems to give syntax errors...
     
  4. mysticwhiskey macrumors newbie

    Joined:
    Mar 31, 2008
    #4
    Hi,

    +[NSString stringWithFormat] will do what you want:

    Code:
    if (licenseDictionary == nil)
        return @"Goodbye!";
    else
        [B]return [NSString stringWithFormat:@"Hello %@", [licenseDictionary objectForKey:@"Full Name"]];[/B]
    
    The %@ symbol applies to objects. The -[NSObject description] message is sent to the object, which for an NString object is equal to the string value.
     
  5. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #5
    works perfectly - thanks mysticwhiskey!
     
  6. mysticwhiskey macrumors newbie

    Joined:
    Mar 31, 2008
    #6
    No worries! :)
     
  7. tacoman667 macrumors regular

    Joined:
    Mar 27, 2008
    #7
    I knew it was something like that. I use StringFormat alot in my C# programming but C# is all dot modifiers. Messaging is completely new to me and I do not know all the static methods in each class yet. Sorry about that!
     
  8. hazmatzak macrumors regular

    Joined:
    Apr 29, 2008
    #8
    If you really are just tacking two strings together, you might also consider -stringByAppendingString
    Code:
    return [@"Hello " stringByAppendingString:[licenseDictionary objectForKey:@"Full Name"]];
     
  9. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #9
    no prob tacoman, thanks for the suggestions, it was worth a shot.

    good to know. thanks hazmatzak.
     

Share This Page