quick code question (noob)

Discussion in 'iOS Programming' started by ryans79, Jun 3, 2009.

  1. ryans79 macrumors regular

    Joined:
    Apr 12, 2009
    #1
    Hello,
    following the code examples in my book i came across this:

    NSArray *components = [self.stateZips allKeys];
    NSArray *sorted = [components sortedArrayUsingSelector:mad:selector(compare:)];
    self.states = sorted;


    I understand the above pretty well, except for this part
    What exactly is that please?

    °°
     
  2. ryans79 thread starter macrumors regular

    Joined:
    Apr 12, 2009
    #2
    needless to say (but will say it anyway), the above smiley face is not there in the code, in the code its

    : )

    but the forum code is converting it into a smiley...
     
  3. grimreaper1377 macrumors regular

    Joined:
    Oct 20, 2007
    #3
    I (think) it's using the compare: method as the basis for comparing items in the array. This way you can customize how elements 'compare' to each other.

    Sorry if I'm wrong - I'm new to this stuff too :)
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    NSArray Class Reference

    I'd suggest, when posting code snippets, to use the
    HTML:
    [CODE]...[/CODE]
    tags, accessible via the # icon in the toolbar, to prevent "smiley conversion".
     
  5. ryans79 thread starter macrumors regular

    Joined:
    Apr 12, 2009
    #5
    Thanks guys!

    This might be old knowledge to you guys but because i am also still in the process of learning cocoa can someone explain to me why the selector has to have a @ symbol before it?

    doing a little docs searching the best i can guess is its telling the compiler this is a obj-c thing... am i right?

    I also thought it weird the "compare" had a colon after it
    Code:
    (compare:)
    and nothing after the colon, i guess i am just using to seeing stuff like

    Code:
    command:(value here)
     
  6. BlackWolf macrumors regular

    Joined:
    Apr 9, 2009
    #6
    because it's a compiler directive I think. anyway, that's just the way the syntax works. why do you put commas in english were you put them? it's how the language works. same here. a selector is always written @selector(something).
    no, there goes nothing after the colon. what you write here is simply the methods signature, nothing more. so "compare" would mean "method compare without any arguments" but "compare:" means "method compare that takes one argument". I give you a few examples, I think that's far more easy. I just post how the method looks in your code and what the selector for it would be
    Code:
    -(void)compare:(NSString *)someString;
    ...
    @selector(compare:)
    
    Code:
    -(void)compare:(NSNumber *)someNumber;
    ...
    @selector(compare:)
    
    Code:
    -(void)compare:(NSString *)someString withNumber:(NSNumber *)someNumber;
    ...
    @selector(compare:withNumber:)
    
    Code:
    -(void)compare;
    ...
    @selector(compare)
    
    got it? ^^
    two important things:
    1) it doesn't matter what type your arguments are. if you have one argument your selector always looks the same
    2) you cannot set the arguments your selector gets! that is impossible! the arguments are predefined by the method that takes the selector, so in this case NSArray's sortedArrayUsingSelector-method. if you want to make a dynamic method call and hand over parameters you need NSInvocation
     
  7. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #7
    What are you using to teach you Cocoa? It seems to me your understanding of some of the basics of Objective-C are still kinda weak. I would suggest stepping away from the real code to revisit and then come back when you feel more comfortable with the language as a whole but still have questions about particular areas.
     
  8. ryans79 thread starter macrumors regular

    Joined:
    Apr 12, 2009
    #8
    @BlackWolf, got it , thanks!

    @dejo, true, it is weak and i confuse myself at times:eek: but i seem to get along usually. I learnt OBJ-C via some screencasts and 2 days with some examples, learning the rest now via the book "Beginning iPhone Development".

    Cheers!
     

Share This Page