Guidelines for naming classes in Objective-C

后端 未结 4 2037
孤街浪徒
孤街浪徒 2020-12-18 07: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, n

相关标签:
4条回答
  • 2020-12-18 08:04

    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

    0 讨论(0)
  • 2020-12-18 08:05

    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.

    0 讨论(0)
  • 2020-12-18 08:10

    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.

    0 讨论(0)
  • 2020-12-18 08:21

    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.

    0 讨论(0)
提交回复
热议问题