Reputation: 1191
I'd like to know how could I set target to my selector. I would like to make this call >>
sortedArray = [views sortedArrayUsingSelector:@selector(compareWithUnit:)];
The problem is my compareWithUnit
function is in LogUnit
class. How should I tell the compiler to look for selector in LogUnit
class ?
views
array contains only LogUnit
(UIViewControllers) objects, and i would like to sort my views
array by [LogUnit dayTime]
result.
- (NSComparisonResult) compareWithUnit:(LogUnit*) unit;
{
return [[self dayTime] compare:[unit dayTime]];
}
Upvotes: 0
Views: 103
Reputation: 69047
From NSArray Reference:
- (NSArray *)sortedArrayUsingSelector:(SEL)comparator
The comparator message is sent to each object in the array and has as its single argument another object in the array.
So, comparator
is expected to be a method of the objects that make up the array. When sorting, each object in the array will be sent that message.
If your views
array contains LogUnit
objects, and LogUnit
defines a compareWithUnit:
method, then it will work. Otherwise, you will get an crash.
There is no way to change this behavior, since it is hardcoded in NSArray
.
By the way, there is no way to include the target in a selector definition, since a target is an object (i.e., an entity existing at runtime), while a selector is just the name of a message you sent to an object.
The idea of adding a class specification to a selector is also flawed for the same reason: a target is an object, not a class. Furthermore, when you send a message to an Obj-C object, you do that disregarding the class of the object, due to the dynamic nature of the language, so it would not help either.
Upvotes: 2
Reputation: 8298
You can also create a selector from a string like:
SEL selector = NSSelectorFromString(@"myMethod:");
Upvotes: -1