Re: how to simulate passing parameter with selector
Re: how to simulate passing parameter with selector
- Subject: Re: how to simulate passing parameter with selector
- From: Ken Thomases <email@hidden>
- Date: Wed, 13 Feb 2008 16:16:00 -0600
On Feb 13, 2008, at 2:51 PM, Daniel Child wrote:
Hi,
I am trying to work around the fact that you cannot pass parameter
values with a selector.
Uh, well, there are plenty of places where you _can_ pass parameter
values with a selector. For example,
performSelector:withObject:withObject:.
Here's what I'm trying to do.
I have a generic table (essentially a class with an array instance
variable) filled with generic records (also a class containing an
array instance variable). I want to be able to sort the table based
on one of the fields in the generic record. Since the generic table
holds an array of records, my instinct is to use sortUsingSelector:
(SEL) s. The problem, of course, is that to set up compare methods,
I need to know which field I'm sorting on, and that is only
determined at runtime.
If you COULD send a parameter, the method call would look something
like:
- (void) sortTableOnField: (int) f {
....
[table records] sortUsingSelector: @selector(compareForFieldIndex: i)}
with the selector somehow transferring the index "i" (the array
index representing a particular field) so that comparisons can be
made.
But (correct me if I'm wrong) you can't send arguments. And since
there could be tons of fields, writing out separate compare methods
(compareFieldOne, compareFieldTwo....) seems dumb.
Is there a way around this?
You want to use sort descriptors, which let you specify the key of the
elements on which to sort.
Alternatively, you can use sortUsingFunction:context:, where you pass
the context of your choice and use it however you like within your
function.
-Ken
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden