function takes string parameter and returns string value

Discussion in 'iOS Programming' started by vidyareef, Jan 20, 2009.

  1. macrumors newbie

    Joined:
    Jan 11, 2009
    #1
    Hello!

    can anyone give me an example of how to write function in xcode that takes on string parameter and after concatenation returns another string value.

    I did a function,
    -(void)Change{
    .
    .
    .
    }

    but this does not return any value and also doesn't pass parameter.

    I tried
    -(NSString *) ChangeCase(NSString* String1){

    }

    but it doesn't work and i am getting errors.
     
  2. macrumors 6502a

    Joined:
    Oct 24, 2008
    Location:
    Cambridge
  3. macrumors regular

    Joined:
    Dec 1, 2007
    #3
    Seconded. But seriously this is really basic Objective-C stuff, not even related to the iPhone SDK. If you don't get a grasp of the basics, you'll never be able to handle the harder stuff yet to come. ;)
     
  4. macrumors regular

    xsmasher

    Joined:
    Jul 18, 2008
    #4
    You were very close. I added a colon, and moved a parenthesis.
    In objective C, the parameters don't go in parentheses.

    Code:
    -(NSString *) ChangeCase:(NSString*) String1{
         upperString = [string1 capitalizedString];
         return upperString;
    }
    
    Call it with
    NSString* newString = [self ChangeCase: @"Hey."];

    Code like this would return an uppercased string. That string is likely set to autorelease though, so retain it if you need to keep it around.

    Note that the usual style is to have variables and methods start with a lowercase letter. So ChangeCase should be changeCase, and String1 should be string1. This will not change the operation, but it makes the code easier to understand.
     

Share This Page