Which code is preferrable to automatically sort NSTableView
This is a CoreData/SQLite app. I have 2 ways to automatically sort the data in a certain way, on applicatio开发者_如何学Cn start-up. Both work as expected, but I would like to ask you which method should be preferred, and why ?
This is the code I made after reading the doc from Apple:
[myTableView setSortDescriptors:[NSArray arrayWithObjects:[NSSortDescriptor sortDescriptorWithKey:@"entity_attribute" ascending:YES selector:@selector(compare:)], nil]];
This is somebody else's code which I found on the web:
NSSortDescriptor *myDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"entity_attribute" ascending:YES selector:@selector(compare:)] autorelease];
NSArray *sortedArray = [NSArray arrayWithObject:myDescriptor];
[myController setSortDescriptors:sortedArray];
Thanks for your advice.
The two samples you list are doing the exact same thing (and, in the exact same way).
Seems like a preference. Maybe the code you saw online, the author thought it was more readable to split it out into three lines, than to have it all on one big line.
精彩评论