无规矩不成方圆

2016-01-25  本文已影响89人  Stark_Dylan

iOS Guidelines

Project Structure

File Structure (.m)

imports

related constants

class extension

@implementation
- dealloc
- init methods
- other
@end

Braces, Asterisk


if (statement) {
    NSLog(@"%@", var);
}

The only exception is a short statement and an immediate return in the beginning:


- (void)performSomeTask {
    if (!user.hasToken) return;
    ...
}

Spaces, Formatting

General


if (pointer != someOtherPointer)

CGFloat result = width * height * 2.f;

BOOL contentExists = self.content.length > 0;

for (int i = 0; i < x; i++) { ... }

Variable declaration

id<NSObject> object = ...;

ClassType *a = (ClassType *)b;,
ScalarType a = (ScalarType)b;

Class Declaration


@interface SCHCategoryViewController : UIViewController <UITableViewDelegate> 

@interface SCHCategoryViewController : UIViewController <
    UITableViewDelegate, 
    UITableViewDataSource, 
    SCHSyncronizationDelegate
>

Method Declaration

- (void)doSomethingWithString:(NSString *)string flag:(BOOL)flag;


- (void)doSomethingWithString:(NSString *)string
                         rect:(CGRect)rectangle
                       length:(NSUInteger)length;

Method Invocation

Function Declaration


void * LongLongLongFunction(
    id firstArgument, 
    id secondArgument,
    id *outArgument,
    int other
)

NSAssert(
    [controller conformsToProtocol:@protocol(EParticipantSelection)],
    @"Controller %@ should confrom to protocol",
    NSStringFromProtocol(@protocol(EParticipantSelection))
);

Access specifiers & ivars


@interface MyClass : NSObject {
    @private
    id _privateIvar;

    @protected
    id _protectedIvar;
}

Property Declaration

Protocol Declaration

Blocks / Closures Declaration


dipatch_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
}];

Naming

In general we're using Apple Coding Guidelines for Cocoa with several highlights:

lowercasePrefix_:
- (void)sch_performTask:(id)arg

Constants

// 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;

Required / recommended best practices

Types Declaration / Usage

Forward Declaration

Use forward declaration whenever possible

Ivars

@public ivars not allowed

Properties

Exceptions handling

Use exceptions only where it is required. Desirable to use NSError ** and / or return result status (i.e. Success, Fail)

Protocols

@protocol CustomClassDelegate <NSObject>

- (NSInteger)someDelegateMethod:(CustomClass *)customClass;
- (void)customClass:(CustomClass *)customClass didSelectRowAtIndexPath:(NSIndexPath *)indexPath;

@end

id<Protocol> instance = ...;, @property (nonatomic, weak) id<Protocol> delegate;

Boolean statements

Preprocessor usage

AppDelegate

Keep your AppDelegate as clean as possible. Any logic, not related to AppDelegate (database seeding, networking, etc) is not allowed

Clean .pch

Group required #defines, constants to a separate header (SCHConstants.h, SCHDefines.h). Garbage in .pch is not allowed

上一篇 下一篇

猜你喜欢

热点阅读