Objective-C 的 constants 也是有學問的
常常是用 #define myConstants var 來處理一些變數,但更好的作法是用 const
// Constants.h |
當希望此變數不為 global 時,則用 static
// Constants.m |
#define 在 code 中是用取代的方式,compiler也沒辦法做 type 檢查,stringInstance == strConstant 的方式也比 #define 中用isEqualToString 快
當要宣告 integer 時,apple 建議用
NSInteger const counter = 0; |
但在loop中會有警告Assignment of read-only variable ‘counter’,需要用以下方式
static int counter = 0; |