iOS Guidelines used in Yalantis ;)
imports
related constants
class extension
@implementation
- dealloc
- init methods
- other
@end
Type *var = ...;
{}
:if (statement) {
NSLog(@"%@", var);
}
The only exception is a short statement and an immediate return in the beginning:
- (void)performSomeTask {
if (!user.hasToken) return;
...
}
if
, while
, for
and similar:if (pointer != someOtherPointer)
CGFloat result = width * height * 2.f;
BOOL contentExists = self.content.length > 0;
for (int i = 0; i < x; i++) { ... }
id<NSObject> object = ...;
ClassType *a = (ClassType *)b;
, ScalarType a = (ScalarType)b;
@interface SCHCategoryViewController : UIViewController <UITableViewDelegate>
@interface SCHCategoryViewController : UIViewController <
UITableViewDelegate,
UITableViewDataSource,
SCHSyncronizationDelegate
>
- (void)doSomethingWithString:(NSString *)string flag:(BOOL)flag;
- (void)doSomethingWithString:(NSString *)string
rect:(CGRect)rectangle
length:(NSUInteger)length;
void * LongLongLongFunction(
id firstArgument,
id secondArgument,
id *outArgument,
int other
)
NSAssert(
[controller conformsToProtocol:@protocol(EParticipantSelection)],
@"Controller %@ should confrom to protocol",
NSStringFromProtocol(@protocol(EParticipantSelection))
);
@public
, @private
, @protected
should be aligned by 1 tab:@interface MyClass : NSObject {
@private
id _privateIvar;
@protected
id _protectedIvar;
}
@property
and opening parentheses 1 space symbol should be used@property (nonatomic, copy, readonly, getter=customGetter) NSString *value;
@required
and @optional
aligned to the line beginningdipatch_async(dispatch_get_main_queue(), ^{
// your code here
});
NSArray *contentArray = nil;
[contentArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop){
// your code here
}];
[UIView animateWithDuration:0.3 animations:^{
// your code here
} completion:^(BOOL finished){
// your code here
}];
^
and return type / arguments: ^(id response){}
^NSUInteger * (id response){}
In general we're using Apple Coding Guidelines for Cocoa with several highlights:
lowercasePrefix_
:
- (void)sch_performTask:(id)arg
extern
keyword:// SCHNotifier.h
OBJC_EXTERN NSString * const SCHNotifierDidChangeStateNotification;
OBJC_EXTERN const CGFloat SCHDefaultAnimationDuration;
// SCHNotifier.m
NSString * const SCHNotifierDidChangeStateNotification = @"com.project.notifier.stateDidChange";
const CGFloat SCHDefaultAnimationDuration = 0.33;
Use forward declaration whenever possible
@public
ivars not allowed
copy
specifier in case mutability leads to unexpected behaviour (i.e. when NSMutableString passed as NSString and mutated outside of the class)readonly
for public properties whenever possible to prevent unexpected class usage (i.e. outlet assigned from client code, etc)nonatomic
whenever possible to speedup code executionUse exceptions only where it is required. Desirable to use NSError ** and / or return result status (i.e. Success, Fail)
sender
argument:@protocol CustomClassDelegate <NSObject>
- (NSInteger)someDelegateMethod:(CustomClass *)customClass;
- (void)customClass:(CustomClass *)customClass didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
@end
id<Protocol> instance = ...;
, @property (nonatomic, weak) id<Protocol> delegate;
if (bar && baz && quux)
Keep your AppDelegate as clean as possible. Any logic, not related to AppDelegate (database seeding, networking, etc) is not allowed
Group required #defines, constants to a separate header (SCHConstants.h, SCHDefines.h). Garbage in .pch is not allowed