Monday, July 2, 2018 [Tweets] [Favorites]

NSOnState Is Deprecated

Jeff Johnson (tweet):

I’m going to discuss one change in the Objective-C API, because it is both illustrative and egregious. In the 10.14 SDK, the Objective-C constants NSOnState, NSOffState, and NSMixedState have been deprecated:

typedef NSControlStateValue NSCellStateValue NS_DEPRECATED_WITH_REPLACEMENT_MAC("NSControlStateValue", 10_0, 10_14);
static const NSControlStateValue NSMixedState NS_DEPRECATED_WITH_REPLACEMENT_MAC("NSControlStateValueMixed", 10_0, 10_14) = NSControlStateValueMixed;
static const NSControlStateValue NSOffState NS_DEPRECATED_WITH_REPLACEMENT_MAC("NSControlStateValueOff", 10_0, 10_14) = NSControlStateValueOff;
static const NSControlStateValue NSOnState NS_DEPRECATED_WITH_REPLACEMENT_MAC("NSControlStateValueOn", 10_0, 10_14) = NSControlStateValueOn;

Notice that these Objective-C constants were introduced in the 10.0 SDK. They’ve been around for the entire history of Mac OS X.

Kevin Ballard:

This change is good even ignoring Swift because it makes the constant consistent with the vast majority of other SDK constants. This makes it more discoverable, easier to remember, and is much better and less confusing for people new to AppKit.

Jeff Johnson:

It seems pretty clear to me that the original Cocoa API designers made the conscious choice to put the most important and informative part of a symbol name first rather than last.

This is a legit philosophy. You may agree or disagree, but in any case it’s consistent.

It was, but they’ve been gradually reversing the order in the names since before Swift existed. In most cases I think this is for the better, although in the interim it means that Cocoa has been internally inconsistent for a long time.

The two points I would add are:

Jeff Johnson:

Seriously, though, imagine a newcomer, how do they learn? A good way is to download sample code. Except the sample code doesn’t get updated frequently, so their first experience with AppKit and Objective-C is deprecation build warnings. How would you feel about that as a learner?

2 Comments

one benefit of the long-prefix names is definitely auto-complete. With NSOnState you cannot expect that autocomplete suggests anything useful. Once you start typing "On" everything is already done. If you have no clue what kind of parameter you are supposed to provide and you start typing the prefix that was already part of the placeholder, the number of suggestions rapidly shrinks, making it much easier to guess what to use.

@Karsten Yes, although this seems like a tooling issue; if the typed parameter is already there, Xcode should be able to suggest something useful.

Stay up-to-date by subscribing to the Comments RSS Feed for this post.

Leave a Comment