**Archived** Style guide & coding conventions for Objective-C projects
1,662
92 commits
updated Nov 8, 2017
These guidelines build on Apple's existing Coding Guidelines for Cocoa. Unless explicitly contradicted below, assume that all of Apple's guidelines apply as well.
nil as a value.#pragma marks to categorize methods into functional groupings and protocol implementations, following this general structure:#pragma mark Properties
@dynamic someProperty;
- (void)setCustomProperty:(id)value {}
#pragma mark Lifecycle
+ (instancetype)objectWithThing:(id)thing {}
- (instancetype)init {}
#pragma mark Drawing
- (void)drawRect:(CGRect) {}
#pragma mark Another functional grouping
#pragma mark GHSuperclass
- (void)someOverriddenMethod {}
#pragma mark NSCopying
- (id)copyWithZone:(NSZone *)zone {}
#pragma mark NSObject
- (NSString *)description {}
readonly properties.readonly if they are only set once in -init.@synthesize unless the compiler requires it. Note that optional properties in protocols must be explicitly synthesized in order to exist.copy if they return immutable objects and aren't ever mutated in the implementation. strong should only be used when exposing a mutable object, or an object that does not conform to <NSCopying>.weak properties whenever possible. A long-lived weak reference is usually a code smell that should be refactored out.@property (attributes) id<Protocol> object;
@property (nonatomic, strong) NSObject<Protocol> *object;
void GHAwesomeFunction(BOOL hasSomeArgs);
instancetype rather than id.CGRectMake()) to C99 struct initialiser syntax. CGRect rect = CGRectMake(3.0, 12.0, 15.0, 80.0);
-init, -dealloc or a custom accessor.NSFileManager.defaultManager).BOOLs.Blah *a = (stuff == thing ? foo : bar);
nil coalescing ternary operators should avoid parentheses.Blah *b = thingThatCouldBeNil ?: defaultValue;
void *ptr = &value + 10 * 3;
NewType a = (NewType)b;
for (int i = 0; i < 10; i++) {
doCoolThings();
}
if bodies with curly braces if there is an else. Single-line if bodies without an else should be on the same line as the if.if (somethingIsBad) return;
if (something == nil) {
// do stuff
} else {
// do other stuff
}
NSError ** argument or send an error on a ReactiveCocoa signal.void.void (^blockName1)(void) = ^{
// do some things
};
id (^blockName2)(id) = ^ id (id args) {
// do some things
};
5 to 5.0, and 5.3 to 5.3f).NSArray *theStuff = @[ @1, @2, @3 ];
NSDictionary *keyedStuff = @{ GHDidCreateStyleGuide: @YES };
NSArray *theStuff = @[
@"Got some long string objects in here.",
[AndSomeModelObjects too],
@"Moar strings."
];
NSDictionary *keyedStuff = @{
@"this.key": @"corresponds to this value",
@"otherKey": @"remoteData.payload",
@"some": @"more",
@"JSON": @"keys",
@"and": @"stuff",
};
Class+Private.2,307 followers · starred Apr 2013
207 followers · starred Jan 2016
442 followers · starred Apr 2013
129 followers · starred Aug 2013
**Archived** Style guide & coding conventions for Objective-C projects
1,662
92 commits
updated Nov 8, 2017
These guidelines build on Apple's existing Coding Guidelines for Cocoa. Unless explicitly contradicted below, assume that all of Apple's guidelines apply as well.
nil as a value.#pragma marks to categorize methods into functional groupings and protocol implementations, following this general structure:#pragma mark Properties
@dynamic someProperty;
- (void)setCustomProperty:(id)value {}
#pragma mark Lifecycle
+ (instancetype)objectWithThing:(id)thing {}
- (instancetype)init {}
#pragma mark Drawing
- (void)drawRect:(CGRect) {}
#pragma mark Another functional grouping
#pragma mark GHSuperclass
- (void)someOverriddenMethod {}
#pragma mark NSCopying
- (id)copyWithZone:(NSZone *)zone {}
#pragma mark NSObject
- (NSString *)description {}
readonly properties.readonly if they are only set once in -init.@synthesize unless the compiler requires it. Note that optional properties in protocols must be explicitly synthesized in order to exist.copy if they return immutable objects and aren't ever mutated in the implementation. strong should only be used when exposing a mutable object, or an object that does not conform to <NSCopying>.weak properties whenever possible. A long-lived weak reference is usually a code smell that should be refactored out.@property (attributes) id<Protocol> object;
@property (nonatomic, strong) NSObject<Protocol> *object;
void GHAwesomeFunction(BOOL hasSomeArgs);
instancetype rather than id.CGRectMake()) to C99 struct initialiser syntax. CGRect rect = CGRectMake(3.0, 12.0, 15.0, 80.0);
-init, -dealloc or a custom accessor.NSFileManager.defaultManager).BOOLs.Blah *a = (stuff == thing ? foo : bar);
nil coalescing ternary operators should avoid parentheses.Blah *b = thingThatCouldBeNil ?: defaultValue;
void *ptr = &value + 10 * 3;
NewType a = (NewType)b;
for (int i = 0; i < 10; i++) {
doCoolThings();
}
if bodies with curly braces if there is an else. Single-line if bodies without an else should be on the same line as the if.if (somethingIsBad) return;
if (something == nil) {
// do stuff
} else {
// do other stuff
}
NSError ** argument or send an error on a ReactiveCocoa signal.void.void (^blockName1)(void) = ^{
// do some things
};
id (^blockName2)(id) = ^ id (id args) {
// do some things
};
5 to 5.0, and 5.3 to 5.3f).NSArray *theStuff = @[ @1, @2, @3 ];
NSDictionary *keyedStuff = @{ GHDidCreateStyleGuide: @YES };
NSArray *theStuff = @[
@"Got some long string objects in here.",
[AndSomeModelObjects too],
@"Moar strings."
];
NSDictionary *keyedStuff = @{
@"this.key": @"corresponds to this value",
@"otherKey": @"remoteData.payload",
@"some": @"more",
@"JSON": @"keys",
@"and": @"stuff",
};
Class+Private.2,307 followers · starred Apr 2013
207 followers · starred Jan 2016
442 followers · starred Apr 2013
129 followers · starred Aug 2013