3 ms·
Nice mustache! Here's some other Foundation classes/methods which are very useful but lesser known: NSCountedSet - and NSMutableSet that keeps counts of object
by tumultco 14y ago
Nice mustache! Here's some other Foundation classes/methods which are very useful but lesser known:
NSCountedSet - and NSMutableSet that keeps counts of objects added
NSValueTransformer - More of a design pattern for generically converting objects
NSArgumentDomain in NSUserDefaults - command line arguments of -key value become user defaults
NSXMLNode nodesForXPath:error:/objectsForXQuery:error: - run XPath or XQuery against XML... even better, subclass NSXMLNode and then use XPath and XQuery on any hierarchical data you have
NSObject's performSelectorOnMainThread:withObject:waitUntilDone: - use NO for waitUntilDone and the method will be called at the end of the event loop (I've seen many people use the afterDelay variant instead)
NSNotificationQueue - use NSNotificationCoalescingOnName to batch up NSNotifications to send a single one at the end of the event loop, though there's lots of gotchyas for using it correctly
NSOrthography - Okay, even I didn't know about this before re-glancing at the foundation reference :)
- mistercow 14y agoIt's interesting how much of the Foundation consists of patterns for overcoming the awkwardness of Objective-C. For example, in most modern languages, NSCountedSet is crazy trivial; you just use a hash of numbers. But since Obj-C doesn't have autoboxing (well, it kinda sorta does nowadays, but anyway), having a built in implementation is nice. NSValueTransformer is a similar workaround for a lack of language features, which will look particularly alien if you're used to languages with proper closures and autoboxing. They are a boilerplate nightmare, and Apple would have done well to provide some simple implementation that could be controlled from the interface builder years ago (based on JavaScript expressions, for example). Anyway, a fun exercise with NSValueTransformers is to build a subclass for chaining them together (bonus points for automatic reversing), and then a subclass of simple primitive operations that you can compose into more complex transformers. This can actually be pretty useful if, for example, you have a number of log-scale sliders and text fields that require slightly different transformation parameters.
- mistercow 14y agoI think my above comment may have come across as unnecessarily harsh against Objective-C. I like Obj-C and I was actually trying to make a broader point: you can often learn a lot about a language's weaknesses and quirks by digging into its standard library.
- melloclello 14y agoOh my god, NSValueTransformer. Why has this not been in my life the past two weeks. Same goes for performSelectorOnMainThread. Thankyou.