Understanding NSString

Discussion in 'iOS Programming' started by Jonse, Jun 10, 2009.

  1. Jonse macrumors newbie

    Joined:
    Dec 22, 2008
    #1
    Hey you guys.

    I'm new to OBjective C and Coca programming, but catching up well and liking the environment.

    Today I encountered a somewhat strange problem though.
    I was writing a simple function that returns a string:

    Code:
    -(NSString *) description
    {
    	return (@"Hello I'm a %@ with %d sides", [self name], [self numberOfSides]);
    }
    Building it gave the warning:
    Warning: return makes pointer from integer without a cast

    If fixed it by using:
    Code:
    -(NSString *) description
    {
    	return [NSString stringWithFormat:@"Hello I'm a %@ with %d sides %@", [self name], [self numberOfSides]];
    }
    
    But why didn't it work in the first place?

    I'm quite curious in understanding the difference between:

    Code:
    return (@"Hello I'm a %@ with %d sides", [self name], [self numberOfSides]);
    and
    Code:
    return [NSString stringWithFormat:@"Hello I'm a %@ with %d sides %@", [self name], [self numberOfSides]];
    Thanks.
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    One tells the NSString class to to something. The other is just a lot of expressions in a set of brackets. This already has a meaning in C. You can't just invent syntax and hope it will work.
     
  3. justfred macrumors newbie

    Joined:
    May 8, 2009
    #3
    In your first example, there is absolutely no invocation of the NSString class methods. In the second example, you invoked the method defined in NSString.m, which takes your parameters, manipulates them, and allocates the string to memory. Your new string now inherits from the NSString class.

    I believe the @"" syntax is just a shortcut, but I think I might be wrong.

    You can do all these things manually, but not the way you attempted to do in your first example. As Robbie said, it just doesn't work.

    In objective C, creating an instance of an object typically uses the methods contained in the object's class to do so.

    Code:
    // you use NSArray class methods to create an array too.
    anArray = [NSArray arrayWithObjects:aDate, aValue, aString, nil];
    
    
     
  4. BlackWolf macrumors regular

    Joined:
    Apr 9, 2009
    #4
    well, @"" is basically a shortcut for creating an NSString, though that isn't really correct, but whatever.

    but if you want to create a DYNAMIC string, so one where you put variables in, you cannot use @"", because @"" is not a method, it doesn't take any arguments, it can't be formatted. that can only be done via NSString's formatWithString method.
     
  5. justfred macrumors newbie

    Joined:
    May 8, 2009
    #5
    oh yeah that makes more sense. It is not a shortcut to make an NSString... but rather it is a shortcut to making a string in objective-C. Thank you.
     

Share This Page