Essence of Cocoa

Thursday, May 1st, 2008

From the documentation for -[NSMutableURLConnection setAllHTTPHeaderFields:]:


A dictionary with the new header fields. HTTP header fields must be string values; therefore, each object and key in the headerFields dictionary must be a subclass of NSString. If either the key or value for a key-value pair is not a subclass of NSString, the key-value pair is skipped.

My reaction was “wait what”. For some reason being this explicit about accepting only NSStrings struck me as really grating at first, and I couldn’t figure out why. I think it’s because I’m used to Objective-C and Cocoa being pretty flexible about type. In a language with runtime typing, it seems like you’d want to use duck typing or some other type of coercion you want, in this case using -description, to get the type you want.

What about you? Do you think this is an “uncocoa-y” method? The Python community has the notion of “pythonic” code, and I definitely think that applies to Objective-C/Cocoa as well, illustrated by this joke I heard once:

“Why do Objective-C programmers make horrible politicians?”

“They don’t retain their delegates.”

Update: Jens Ayton pointed out via Twitter that -description is the wrong thing. -stringValue is more along the lines of what I was thinking of, which was Python’s str() function. -description is analogous to Python’s __repr__() member function. Can you tell I’ve been writing a lot of Python lately?


  1. Jens Ayton replied on May 2nd, 2008:

    Minor heads up: I just added -stringValue support in JAPropertyListExtractors, and noticed that NSDate (which I was already special-casing, along with NSNumber) does not support it.

  2. Colin Barrett replied on May 2nd, 2008:

    Since Google didn’t turn up anything for JAPropertyListExtractors, I’m linking to it here. Hopefully this page gets indexed.

  3. Todd Dithendorf replied on May 9th, 2008:

    cbarrett, I agree… seems very uncocoa-y to me. Sounds like the Foundation team hired some Java programmers… ;0]