Objective-C Class Prefixes [closed]

痞子三分冷 提交于 2019-12-04 00:48:37

My general approach is to prefix class names that are part of a framework or loadable bundle i.e. classes that might be shared amongst several applications and with other frameworks, but not to bother with classes that appear as part of a standalone application.

If Steve Jobs granted me one wish it would be to have name spaces in Objective-C 3.0 (which would be available tomorrow).

itsaboutcode

There is a very good guidline by Scott Stevenson on how objective-C code should look like. Checkout the following links.

http://cocoadevcentral.com/articles/000082.php
http://cocoadevcentral.com/articles/000083.php

These links will also answer your question how you should name your classes and why.

I use a prefix, even in application code that won't be shared -- mostly for consistently. I generally use a 2-letter abbreviation for the app or framework name in which the code originated, unless a different prefix (e.g, 3 letters, or a short descriptive word) makes more sense.

you definitely should prefix them. if there is a collision, the behaviour is undefined.

what actually happens (last i ran into this) is that the binary is loaded, but your class is not loaded if another (objc) class with that name has already loaded. i'll let you figure out which implementation you'll get when you create an instance of this class ;) such a collision will likely result in a crash or a lot of swallowed exceptions (and a non-functional app). a lot of developers use 2 uppercase letters, which is (all things being equal) 26*26 chance that they will use the same prefix. again - this has happened to me soooo.... it is best that you do it to avoid rewriting a lot of code later on.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!