Guidelines for naming classes in Objective-C

2019-01-25 17:48发布

First of all, there are no namespaces in Objective-C, that's one thing. But when a project increases in size and files, and UITableCellViews and other subviews are added, naming my classes tend to become a real pain..

For example using a model named EEMSystem in a table, my natural way to name the custom UITableViewCell would be something like EEMSystemTableViewCellController.m. This creates really long class names..

Are there any guidelines for naming controllers, views and models? What guidelines are you using?

4条回答
祖国的老花朵
2楼-- · 2019-01-25 18:23

for subclasses besides UIViewController, (like custom table cells, or UIView's not associated with a vc), I use things like CustomCell.m or LoadScreenView.m etc., not sure if its standard, but it works and helps with not having 200 letter class names.

查看更多
看我几分像从前
3楼-- · 2019-01-25 18:31

Scott Stevenson has some recommended guidelines for class naming here:

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

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

From that article:

Whether they're a standard part of Cocoa or your own creation, class names are always capitalized.

Objective-C doesn't have namespaces, so prefix your class names with initials. This avoids "namespace collision," which is a situation where two pieces of code have the same name but do different things. Classes created by Cocoa Dev Central would probably be prefixed with "CDC".

If you subclass a standard Cocoa class, it's good idea to combine your prefix with the superclass name, such as CDCTableView.

查看更多
欢心
4楼-- · 2019-01-25 18:35

The Coding Guidelines for Cocoa have some basic advice on naming conventions in Cocoa, but it mostly relates to method names. Generally, it's not unusual that names in Cocoa are pretty long.

In your example, I would name the class either EEMSystemTableViewCell or simply EEMSystemCell. EEMSystemTableViewCellController would imply that the class is a controller although it's actually a view.

查看更多
时光不老,我们不散
5楼-- · 2019-01-25 18:40

Stylish Objective-C programmers always prefix their classes. The prefix is typically related to the name of the application you are developing or the library that it belong to. For example, if I were writing an application name "ImageViewer", I would prefix all classes with IMV. Classes that you will use across multiple projects typically bear a prefix that is related to your name (TCT), your company's name, or a portable library (MAP, APN). Notice that Apple's classes have prefixes, too. Apple's classes are organized into frameworks, and each framework has its own prefix. For instance, the UIButton class belong to the UIKit framework. The classes NSArray and NSString belong to the Foundation framework. (The NS stands for NeXTSTEP, the platform for which these classes were originally designed.) For your classes, you should use three letters prefixes. Two letters prefixes are reserved by Apple for use in framework classes. Although nothing is stopping you from creating a class with two letters prefix, but you should use three letters to eliminate the possibility of namespace collisions with Apple's present and future classes. Please check it out for details: IOS - Objective-C - Class Names

查看更多
登录 后发表回答