Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ryans79

macrumors regular
Original poster
Apr 12, 2009
226
0
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
@selector(compare:)
What exactly is that please?

°°
 
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...
 
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 :)
 
- (NSArray *)sortedArrayUsingSelector: (SEL)comparator

Parameters
comparator
A selector that identifies the method to use to compare two elements at a time. The method should return NSOrderedAscending if the receiver is smaller than the argument, NSOrderedDescending if the receiver is larger than the argument, and NSOrderedSame if they are equal.

NSArray Class Reference

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...
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".
 
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)
 
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)

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
 
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?
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.
 
@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!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.