(NSString*) ??

Discussion in 'iOS Programming' started by thedon1, Jul 2, 2012.

  1. thedon1 macrumors 6502

    Joined:
    Jun 26, 2010
    #1
    I've recently started learning Objective C and started making some basic things in Xcode.

    It's been going pretty well the last couple of weeks but i'm stuck with something.

    I know that NSString *myString would be making an instance of the object NSString called myString.

    What does it mean when code is written as (NSString*) myString?

    I don't get what the brackets are for and why the asterisk is inside the brackets. I just used NSString as an example but i've seen it with other things.

    I've seen this in some example code but want to understand it before moving on.




    It's probably a really basic question so i'm sorry but it's my first OO Language.


    Thanks
     
  2. animefx, Jul 2, 2012
    Last edited by a moderator: Jul 2, 2012

    animefx macrumors regular

    Joined:
    May 10, 2005
    Location:
    Illinois
    #2
    In your example most likely something is being passed to a function or class.

    so normally you might have something like:

    Code:
    myFunction(int x, int y)
    {
    
    }
    ...but when a function is expecting an instance, then it's written like:

    Code:
    myFunction(NSString *) myString
    {
    
    }
    I'm not sure why it's not (NSString* myString) instead, but I think it could be and would work fine. Someone correct me if I'm wrong.
     
  3. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #3
    This is wrong.

    A function expecting an NSString* looks like this:
    Code:
    myFunction(NSString * myString)
    {
    
    }
    
    However, a method expecting an NSString* looks like this:
    Code:
    myMethod:(NSString *) myString
    {
    
    }
    
    If you don't know the difference between a function and a method, you should review the fundamentals.

    http://developer.apple.com/library/...ptual/OOP_ObjC/Introduction/Introduction.html
    http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/ObjectiveC/

    ----------

    No it doesn't "make an instance". It declares a variable named myString.


    You have to post the complete code, not just an isolated fragment.

    I can think of at least two completely different cases:
    Code:
    NSString * aString = (NSString *) anotherObject;
    
    someMethod: (NSString *) myString;
    
    The first is a type cast. The second is a method declaration.

    What book or tutorial are you learning from? Post the exact title, author, edition and/or URL.


    Exactly what code? Post a url.

    And those are parentheses, not brackets.
     
  4. thedon1 thread starter macrumors 6502

    Joined:
    Jun 26, 2010
    #4
    Thanks for the reply mate. Here's the code i'm using.

    It's a method that basically get's the value from a slider I have and assigns it to a textfield.

    Code:
    -(IBAction)sliding:(id)sender;
    {
    
        UISlider *s = (UISlider*) sender;
        
        int value = (int) s.value;
        NSString *newLabel = [[NSString alloc]initWithFormat:@"%i",value];
        sLabel.text = newLabel;
    }
    
    I understand it all apart from
    Code:
    UISlider *s = (UISlider*) sender;

    Given your previous post, it looks like it's casting sender as a UISlider and then passing it to s.

    this makes sense as sender is an id type.

    Is this correct?
     
  5. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #5
    Correct. That use is a type cast.

    However, it's not "passing" it to s. It's "assigning" it to s. Or one could say "storing it in s".

    "Passing" usually refers to a value given as the argument (parameter) to a function or method. So value is passed to the initWithFormat: method, but "s.value" is assigned to the value variable in the line prior to that.
     
  6. thedon1 thread starter macrumors 6502

    Joined:
    Jun 26, 2010
    #6
    Thanks for taking the time to reply, that really cleared that up.

    One follow up question. If i didn't use the variable s, could I still get to the exact same result, just with a bit more code?

    Could the first 2 lines of code be written in one (longer) line?
     
  7. MattInOz macrumors 68030

    MattInOz

    Joined:
    Jan 19, 2006
    Location:
    Sydney
    #7
    Do you mean...
    Would this still work?

    First thing to ask is "if I experiment and it goes wrong what is the worst that will happen?"
    The answer to me seems to be 9 times out of 10 is it'll crash. Just make sure you comment out your working code so you can revert if it doesn't work. The system seems to be pretty forgiving of exploration. I've haven't managed to kill anything other than the program I was trying to write. Believe me I've done some stuff that was really dumb in hindsight.

    So read up on Type Casting plus also get an understanding of what an "id" type is, then decide if you think this will work and why.

    Code:
    -(IBAction)sliding:(id)sender;
    {
        sLabel.text = [[NSString alloc]initWithFormat:@"%i",(int) [(UISlider *)sender value]];
    }
    
    Once you've come to a conclusion then type it in, see if the compiler is happy with it. If so see if it still produces the correct result when running.
     

Share This Page