having a function return a string

Discussion in 'Mac Programming' started by pendal, May 19, 2009.

  1. pendal macrumors newbie

    pendal

    Joined:
    May 11, 2009
    #1
    I am new to cocoa / objective-C .. and I am trying to make this work.

    this is my function , that i thought would work.

    all i want this function to do right now is return a text string

    PHP:

    -(char)actionComp{

     
    char message;
     
    message = @"The Files at location ",archSource, @"Will be compressed to ",archDest;

    return 
    message;

    }

    when i try to build and go i get the warning
    "assignment makes integer from pointer without a cast."
    on the message = line


    the two variables archSource and archDest are listed as this in the .h file.

    PHP:
    char *archSource,*archDest;
    }

    @
    property(readwritearchSource,archDest;


    Can anyone help?

    Thanks,
    P
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    The construct @"My String" creates an NSString object. It is not a char. You need to alter your function to declare the correct return type (in this case (NSString *)). Even if @"My String" created a C-style string that would be a (char *) not a char.

    Edit to add: you also need to alter you message variable. It needs to be NSString * not char. And I've no idea what you think the construct @"String",variable,@"string" is doing. It's certainly not doing string concatenation. I suggest you need to go right back to basics, read the documentation and learn the language before you try and write any code.
     
  3. pendal thread starter macrumors newbie

    pendal

    Joined:
    May 11, 2009
    #3
    yeah i know .. i'm just trying to wrap my head around this language and can't seem to get anywhere.
     
  4. vishvesh macrumors newbie

    Joined:
    Mar 17, 2008
    Location:
    India
    #4
    Download cocoa browser, it should make your life a little easier.
     
  5. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #5
    I don't know that it will help much at this point, but:
    Code:
    -(NSString *)actionComp{
      return [NSString stringWithFormat:@"The Files at location %@ will be compressed to %@",archSource,archDest];
    }  
    
    This is also assuming that archSource and archDest have been changed to NSString *, which makes more sense. Also, the NSString that is returned is autoreleased since actionComp doesn't contain the words new, copy, or alloc.

    Here is what your original code is equivalent to:
    Code:
    -(char)actionComp{
      return (char)archDest;
    }
    
    archDest is a char *, so the value of the pointer will be cast down to char, hence the warning. There's about a 50/50 chance the char you end up with will be unprintable.

    Here's the link to the NSString page:
    http://developer.apple.com/document...lasses/NSString_Class/Reference/NSString.html

    But you should probably start here:
    http://developer.apple.com/document.../ObjectiveC/Introduction/introObjectiveC.html

    -Lee
     

Share This Page