Compare method
Either you implement a compare-method for your object:
- (NSComparisonResult)compare:(Person *)otherObject {
return [self.birthDate compare:otherObject.birthDate];
}
NSArray *sortedArray = [drinkDetails sortedArrayUsingSelector:@selector(compare:)];
NSSortDescriptor (better)
or usually even better:
NSSortDescriptor *sortDescriptor;
sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"birthDate"
ascending:YES];
NSArray *sortedArray = [drinkDetails sortedArrayUsingDescriptors:@[sortDescriptor]];
You can easily sort by multiple keys by adding more than one to the array. Using custom comparator-methods is possible as well. Have a look at the documentation.
Blocks (shiny!)
There's also the possibility of sorting with a block since Mac OS X 10.6 and iOS 4:
NSArray *sortedArray;
sortedArray = [drinkDetails sortedArrayUsingComparator:^NSComparisonResult(Person *a, Person *b) {
return [a.birthDate compare:b.birthDate];
}];
Performance
The -compare:
and block-based methods will be quite a bit faster, in general, than using NSSortDescriptor
as the latter relies on KVC. The primary advantage of the NSSortDescriptor
method is that it provides a way to define your sort order using data, rather than code, which makes it easy to e.g. set things up so users can sort an NSTableView
by clicking on the header row.
Ideally an object would start observing interesting events as soon as its initialized. So it will register all interesting events with the NotificationCenter inside its initialization code. sendEvent:
is basically a wrapper around the postNotification:
method.
@implementation A
- (id)init {
if(self = [super init]) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveEvent:) name:@"SomeEvent" object:nil];
}
return self;
}
- (void)sendEvent {
[[NSNotificationCenter defaultCenter] postNotificationName:@"SomeOtherEvent" object:nil];
}
// Called whenever an event named "SomeEvent" is fired, from any object.
- (void)receiveEvent:(NSNotification *)notification {
// handle event
}
@end
Same for class B.
Edit 1:
You might be over-complicating the problem. A NSNotificationCenter acts as a broker to whom all events are sent and it decides who to forward that to. It's like the Observer pattern but objects don't directly observe or notify each other, but rather through a central broker - the NSNotificationCenter in this case. With that you don't need to directly connect two classes that might be interacting with each other with an #include
.
While designing your classes, don't worry about how an object would get notified or how it would notify other interested objects, only that an object needs to get notified about some events when they occur, or it needs to inform NSNotficationCenter of its events when they occur.
So in short, find out all events that an object should know about and register those events in this init()
method, and unregister them in the dealloc()
method.
You may find this basic tutorial helpful.
Best Answer
... somewhere else in another class ...