Add Pragma guidance to Objective-C style guide
Adds guidelines for how to delineate sections of an implementation file using #pragma mark. Change-Id: I11f8291e90404e6a0fd62ca7e08738d5455aa6f3 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3443380 Reviewed-by: Gauthier Ambard <gambard@chromium.org> Commit-Queue: Mark Cogan <marq@chromium.org> Cr-Commit-Position: refs/heads/main@{#969498}
This commit is contained in:

committed by
Chromium LUCI CQ

parent
6ac1cb2239
commit
b883d23f3b
@@ -27,6 +27,70 @@ to Chromium Objective-C and (especially) Objective-C++
|
|||||||
Use `nil` for null pointers to Objective-C objects, and `nullptr` for C++
|
Use `nil` for null pointers to Objective-C objects, and `nullptr` for C++
|
||||||
objects.
|
objects.
|
||||||
|
|
||||||
|
## Delineate interface implementations with `#pragma mark -`
|
||||||
|
|
||||||
|
To keep implementation files organized and navigable, method implementations
|
||||||
|
should be clearly grouped by the interfaces they implement, and each such group
|
||||||
|
should be marked with a `#pragma mark -`, followed by the interface name (see
|
||||||
|
examples below). This kind of grouping should happen for the implementations's
|
||||||
|
public methods, superclass methods, private methods, and each protocol the
|
||||||
|
implementation conforms to. In each group, methods should appear in the same
|
||||||
|
order they are defined in the corresponding interface declaration. (This is less
|
||||||
|
important for superclass methods for UIKit subclasses; it's not a big deal if
|
||||||
|
the ordering of `UIViewController` subclass methods doesn't match the UIKit
|
||||||
|
header).
|
||||||
|
|
||||||
|
```objective-c
|
||||||
|
@interface ExampleViewController : UIViewController<ExampleConsumer,
|
||||||
|
UITableViewDelegate>
|
||||||
|
|
||||||
|
...
|
||||||
|
@end
|
||||||
|
|
||||||
|
@implementation ExampleViewController
|
||||||
|
|
||||||
|
- (instancetype)init {
|
||||||
|
...
|
||||||
|
}
|
||||||
|
|
||||||
|
#pragma mark - Public Properties
|
||||||
|
|
||||||
|
- (NSString*)stringProperty {
|
||||||
|
...
|
||||||
|
}
|
||||||
|
|
||||||
|
... // Other properties
|
||||||
|
|
||||||
|
#pragma mark - UIViewController
|
||||||
|
|
||||||
|
- (void)viewDidLoad {
|
||||||
|
...
|
||||||
|
}
|
||||||
|
|
||||||
|
... // Other superclass methods
|
||||||
|
|
||||||
|
#pragma mark - UITableViewDelegate
|
||||||
|
|
||||||
|
... // Protocol methods
|
||||||
|
|
||||||
|
#pragma mark - ExampleConsumer
|
||||||
|
|
||||||
|
... // Protocol methods
|
||||||
|
|
||||||
|
#pragma mark - Private methods
|
||||||
|
|
||||||
|
... // Private methods
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
Private methods can be grouped differently if it helps make the code more
|
||||||
|
readable; for example, private methods that are just helpers for methods in
|
||||||
|
specific protocols could be grouped under a `#pragma mark - <Protocol> helpers`,
|
||||||
|
directly after the protocol methods, instead of with the other private methods.
|
||||||
|
|
||||||
|
Remember that any method which isn't declared in an interface needs a full
|
||||||
|
method comment.
|
||||||
|
|
||||||
## Objective-C++ style matches the language
|
## Objective-C++ style matches the language
|
||||||
|
|
||||||
Within an Objective-C++ source file, follow the style for the language of the
|
Within an Objective-C++ source file, follow the style for the language of the
|
||||||
|
Reference in New Issue
Block a user