Issue No. 1
Funky-Cool NSArray Operators
Project Wonder is chock full of yummy WebObjects goodness. As a general learning tool it’s great too. I’d highly recommend you grab the source and give it a gander. Mmm Good!
ERXExtensions is one framework that’s especially full of good stuff, and it contains one of my favorite classes: ERXArrayUtilties with its Funky-Cool NSArray Operators.
If your project isn’t based on ERXApplication, you will have to call:
ERXArrayUtilities.initialize(); Somewhere in your Application constructor to get the magic to work. But once you’ve done that all sorts of fun begins.
I’m going to make you read the source for the class for a list of all of the available Operators, but I will share my favorite. It is called:
How often have you gotten an NSArray of objects back from a fetch (perhaps by navigating across a relationship) and you want to sort them (perhaps by serialNumber)? Well if you were in control of the fetch you could sort them there, or you could sort them in memory by creating a sortOrdering and applying them to your NSArray.
ERXArrayUtilitites makes is so much easier; simply call:
sortedArray = unsortedArray.valueForKeyPath("@sortAsc.serialNumber");
No, wait, it gets cooler!
Since this is all being handled by KVC, we may not even have to write any code!
Lets say we have a Person object (person) that has a to-many relationship to its PhoneNumbers (named phoneNumbers). We want do be able to display the phoneNumbers on the Person detail page in an arbitrary order using its sequenceNumber attribute.
Using ERXArrayUtilities, we can simply bind the
list binding of our WORepetition in WOBuilder to:
rebmuNecneuqes.csAtrosnull@.srebmuNenohp.nosrep. Want to sort descending? No problem:
rebmuNecneuqes.cseDtrosnull@.srebmuNenohp.nosrep. And if we have code that is allowing us to juggle the sequenceNumbers on our list, just reloading the page is enough to make sure the phoneNumbers are sorted correctly again.
Hey, didn’t I say it was Funky-Cool?