25 Apr 2009

To trim a string and remove whitespace and newlines:

NSString *trimmedString = [dirtyString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

For other character sets, see this reference page.

String trim in Objective-C/Cocoa

25 Apr 2009

Jeff Lemarche, one-half of the dynamic duo behind the excellent Beginning iPhone Development book and also part-time Twitter prankster has an interesting post up on Differences in Delegation between Cocoa and Cocoa Touch (or, more specifically, a review of the merits of using formal vs. informal protocols for delegates).

Today, I ran across a use case where, unless I'm missing something, using an informal protocol is the only viable solution. Take this scenario:

(more...)

On specializing the protocol of a delegate in the subclass of an abstract class

25 Apr 2009

It appears that having your protocol conform to the NSObject protocol (@protocol MyProtocol <NSObject>) won't help you if you need to send performSelectorOnMainThread:withObject:waitUntilDone: messages to objects that conform to it (e.g., id myObj;). That's because the NSObject protocol doesn't declare that method.

Instead, declare the type of your objects as NSObject conforming to your protocol like this:

(more...)

NSObject protocol doesn’t include performSelectorOnMainThread: withObject: waitUntilDone: method

24 Apr 2009

  • If you've ever tried to setup unit tests in Xcode, you'll know what a PITA they can be, especially when it comes to trying to debug your tests. Gabriel Handford’s GHUnit is a unit testing framework with its own user interface, and is well worth a look. You can make tests by subclassing GHTestCase, but it also supports SenTestCase and GTMTestCase subclasses, and I was able to get the ASIHTTPRequest tests up and running in no time at all. There’s even a UI for running tests in the iPhone simulator...

    (more...)

Links for 2009-04-23

23 Apr 2009

Links for 2009-04-22