Leading Underscores

Have written something that you’d swear you’d never have written because it’s wrong and you know it’s wrong? But you did anyway? In Chapter 3, I said that Apple reserves leading underscores in ivar names. This isn’t true. Apple reserves leading underscores in method names, but not ivar names.

While I believe avoiding a leading underscore was valuable in the past for the other reasons listed (particularly avoiding some KVC magic), with the move to auto-synthesized ivars this recommendation has become more trouble than its worth. In the future, our recommendation is to follow Xcode’s lead: readwrite ivars should have a leading underscore. readonly ivars should have a leading double-underscore. This double-underscore also fixes the KVC concern, so it wins in several ways.

Thanks to Mark Dalrymple (@borkware) for pointing out this issue.

Leave a Reply

Your email address will not be published. Required fields are marked *