Re: Cocoa Foundation/com.webobjects.foundation

Paul Lynch <[email protected]> Thu, 27 Apr 2006 16:19:18 +0100
Newsgroups gmane.comp.web.webobjects.newbies
Message-ID <[email protected]>
On 27 Apr 2006, at 14:59, Graham J Lee wrote:

> ...or "how sorting an NSArray made me go bald".
>
> Has anyone written a guide to the differences between Cocoa's  
> Foundation and the WO not-quite-variant?  A quick guide to the  
> changes would probably fix most of the scrapes I get myself into;  
> such as where Comparable/Comparator/NSComparators are used instead  
> of functions, where java.lang classes get used instead of  
> Foundation equivalents and so on.

The simple, cut-down answer, is that in WebObjects you will almost  
always use EOSortOrdering.sortArrayUsingKeyOrderArray() or  
EOSortOrdering.sortedArrayUsingKeyOrderArray().  These are intended  
to operate on EOs in your array, but will work for anything  
supporting KVC, like NSDictionary, etc.

The NSArray methods, sortedArrayUsingComparator and  
sortedArrayUsingSelector are rarely used, and are for when you want  
to use non-KVC classes in your arrays, like plain java classes  
(Integer, etc).  Note that the selector methods are deprecated, so  
you don't want to use then - stick to NSComparator.  For a lot of  
standard classes, you can use the static comparators included in the  
NSComparator class, for Strings, Numbers and NSTimestamps, like  
NSComparator.AscendingCaseInsensitiveStringComparator.  To use  
NSComparator for more advanced cases, which means comparing less  
usual classes or different class types, you just create a new class  
extending NSComparator, and implement compare:, which should use the  
various class constants as return values.

Comparable and Comparator (and java.util.Arrays sorting functions)  
are typical java kludges for sorting arrays/lists, to get around the  
fact that java has never really had a proper Array class.  You'll be  
forced to use them if you need to write code that can be used outside  
of a WebObjects context, like if you need to create pojos/beans.   
NSComparator is a revision to make the WebObjects approach closer to  
the approach used in Java in the outside world.

In short:

For EOs or dictionaries, use EOSortOrdering methods.

For String, Number subclasses or NSTimestamp, use the predefined  
NSComparator classes.

For anything else, implement your own NSComparator subclass.

Paul



 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/webobjects-newbies/

<*> To unsubscribe from this group, send an email to:
    [email protected]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/