5

Click here to load reader

SurveyiOSCodingStyleFromAppleDocument

Embed Size (px)

Citation preview

Page 1: SurveyiOSCodingStyleFromAppleDocument

Survey iOS Coding Style From Apple Document: (請參考 此文章)

1. Code Naming Basics(基本命名法則):

General Rules: 採用 verb 當作一個 method 的 prefix 無需採用 ”get” 這個 prefix 採用 keywords before all arguments

­ (void) sendAction: (SEL) aSelector toObject: (id) anObject forAllCells: (BOOL) flag;

Method 的命名需要採用一些字去描述 argument, 並且放在 argument 之前 ­ (id) viewWithTag: (NSInteger) aTag;

只有一個情況需要用 “and” 當作 keyword 去連接, 就是將 and 用於連接

兩個 actions ­ (BOOL) openFile: (NSString*) fullPath withApplication: (NSString *) appnam andDeactivate:(BOOL) flag;

Class and Protocol Names 大部分 protocol (或稱 interface 在 java) 相關的 methods 並不會被實作

出來, 所以為了區別 class 和 protocol, 通常用 a gerund (...ing) 去當作

名稱. 請看以下程式碼: @protocol Drawing @required // 一定要被 implement ­ (void) draw ­ (void) changeColor; @optional // 未必要被 implement ­ (void) whateverMethod; @end

2. Naming Methods(命名方法): 採用 Accessor Methods (set 和 return the value of a property of an object )

if the property is expressed as a noun, then the format is: ­ (type) noun: ­ (void) setNoun: (type) aNoun;

if the property is expressed as an adjective, then the format is: ­ (BOOL) isEditable; ­ (void) setEditable: (BOOL) flag;

if the property is expressed as a verb, then the format is: (注意: The verb should be in the simple present tense.) ­ (BOOL) showsAlpha; ­ (void) setShowsAlpha: (BOOL) flag;

Page 2: SurveyiOSCodingStyleFromAppleDocument

採用 Delegate Methods: 這個 class 的名稱無需 omits the prefix and the first

letter is in lowercase. Delegate methods(or delegation methods) 常用於一個 class 呼叫它自

己的 delegate 去告知 users 資料有變化: 1. 當實做的部分會寫在 delegate 的 methods 中 2. 無需加入 method prefix name

­ (BOOL) applicationOpenUntitleFile:(NSApplication*) sender; ­ (void) windowDidChangeScreen:(NSNotification*) notification

3. 採用 'did' or 'will' 代表事情 has happened or is about to happen, 也可以採用 'should' 來表示將發生的未確定 ­ (void) BrowserDidScroll: (NSBrowser *) sender;

採用 Collection Methods

對於用 convention 的方式去維護一個 collection, 通常會有以下的 methods 出現 ­ (void) addElement: (elementType) anObj; ­ (void) removeElement: (elementType) anObj; ­ (NSArray *) elements;

例如: ­ (void) addLayoutManager: (NSLayoutManager *) obj; ­ (void) removeLayoutManager: (NSLayoutManager *) obj; ­ (NSArray *) layoutManagers;

當遇到的 collection, 為一個 truly un­order, 則採用以下的方法 (NSSet 而非 NSArray) ­ (void) insertLayoutManger: (NSLayoutManager*) obj atIndex:(int)

index ­ (void) removeLayoutManagerAtIndex: (int) index

採用 Modal verbs (例如: can, should, will, 等等) 去清楚表示意思, 千萬別用 do 或 does.

如何取名 Private Methods: 千萬別用 underscore character as a prefix name for your private

method. 如果你大量的運用 subclasses 從 NSView, UIView, 可以採用公司名稱

或案件名稱當作開頭, 假設 project/company name is called as Byte Flogger, 就命名為 BF_addObject:

3. Naming Function(取名函數):

如果 return 得值為, the property of its first argument, omit the verb ­ float NSHeight(NSRect aRect);

如果 return 為一個 reference, 則採用 "GET" ­ const char *NSGetSizeAndAlignment(...)

4. Naming Properties and Data Types (取名 properties 和資料型態) Declared property

型態常表示為 @property (...) type nounOrVerb For example:

Page 3: SurveyiOSCodingStyleFromAppleDocument

@property (strong) NSString *title; @property (assign) showsAlpha;

如果 declared property 為一個 adjective, 必須 omits the “is” prefix @property (assign, getter=isEditable) BOOL editable; 改變 editable 的名稱為 isEditable

如果宣告一個 property 通常也會 synthesize(自動產生 setter 和 getter) 一個相關的 instance variable(簡寫為: ivar). Instance variable 通常是用

於一個 class 本身或是它的 subclass. 然而 @property 用於將 variable 變成 public. @synthesize 為自動加入 setter 和 getter 給指定的 variable. 在 .h file: @property (strong) BOOL showsTitle; 在 .m file: @implement MyClass

BOOL _showsTitle; @synthesize showsTitle = _showsTitle

Constants(常數): 有兩種方式宣告常數, 一種為 enum 另一種為 constant. Enumerated constants

Typedef enum _NSMatrixMode NSRadioModeMatrix = 0, NSHighlightModeMatrix = 1, NSListModeMatrix = 2, NSTrackModeMatrix = 3,

NSMatrixMode; 或者是 Enum

NSBorderlessWindowMast = 0, NSTitleWindowMask = 1 << 0, NSClosableWindowMask = 1 << 1, NSMiniaturizableWindowMask = 1 <<2, NSResizeableWindowMask = 1<<3

Constant created with const 當確定此變數將不會相關於其他變數使用, const float NSLightGray;

5. Tips and Techniques for Framework Developers (常見的開發問題): Class Initialization: 以下展示如何 initialize 一個物件採用以下 executed once,

lazily, before any other method of the class is invoked. + (void) initialize

static dispatch_once_t onceToken =0; dispatch_once(&onceToken, ^

// the initializing code

Page 4: SurveyiOSCodingStyleFromAppleDocument

)

Designated Initializers: 通常稱 init method 為一個 designated initializer, 它會

初始化 superclass. ­ (id) init

self = [super init]; // Call a designated initializer here If (self != nil)

// Initialize object … If (someError) // 如果發生問題, 立即釋放 object

[self release]; self = nil;

ResponseToSelector: 是一個 runtime checks, 用於確認此 method 是否還存

在, 因為有些 methods 會再版本 increment 後被 remove 掉.

Bitfields: 千萬別用 signed value for bitfields, 尤其在於 one­bit 情況下, 因為可

能會導致 undefined behaviour. One bit bitfield 要永遠保持 unsigned value. 因為只有 0 和 ­1, 可以儲存在 bitfield 而已. BOOL isAttachment:1; ← 宣告此變數為只有一個位元的變數 int startTracking:1; ← 應該要改成 unsigned int. 不然會出現錯誤

Memory Allocation: 通常會先用 stack 的方式去存取資料, 也就是 array, 如果資

料滿了以後, 才會採用 heap 的方式, 也就是 malloc 的方式去擴大並且存取資

料. 整理一下 STACK V.S. HEAP:

1. Stack Pros and Cons: a. Very fast access b. Don’t have to explicitly de­allocate variables c. Space is managed by CPU, memory will not become

fragmented d. Local variables only e. Limit on stack size (OS­dependent) f. Variables cannot be resized

2. Heap Pros and Cons: (malloc(), calloc(), realloc(), free()) a. Variables can be accessed globally b. Not limit on memory size c. (Relatively) slower access d. No guaranteed efficient use of space, memory may

become fragmented over time as blocks of memory are allocated, then freed

e. Variables can be resized using realloc()

Page 5: SurveyiOSCodingStyleFromAppleDocument

程式碼為以下: #define STACKBUFSIZE (1000 / sizeof (YourElementType)) YourElementType stackBuffer[STACKBUFSIZE]; YourElementType *buf = stackBuffer; int capacity = STACKBUFSIZE; int numElements = 0; while (1)

If (numElements > capacity) // Need more room Int newCapacity = capacity * 2; If (buf == stackBuffer)

buf = malloc (newCapacity * sizeof(YourElementType)); memove(buf, stackBuffer, capacity * sizeof (YourElementType));

else Buf = realloc(buf, newCapacity * sizeof(YourElementType))

Capacity = newCapacity;

// … use buf; increment numElements

// … If (buf != stackBuffer) free(buf);

6. 補充資料: iOS 的考題