Thursday, May 1st, 2008
From the documentation for
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
-description is analogous to Python’s
__repr__() member function. Can you tell I’ve been writing a lot of Python lately?