#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kAnimationDuration 0.25
k = constant(常量),源自匈牙利命名法的變體,蘋果內部代碼規範(Apple Coding Guidelines)早期廣泛使用,後被社區沿用。
| 前綴 | 含義 | 示例 |
|---|---|---|
| k | 常量 | kMaxCount, kDefaultColor |
| g | 全局變量 | gUserSession |
| s | 靜態變量 | sSharedInstance |
| m_ / _ | 成員變量 | _userName, mViewController |
| p | 指針 | pBuffer |
static const / constexpr (C++) 或 FOUNDATION_EXPORT const (Objective-C)let,無需前綴k 風格一致即可// Objective-C 現代寫法
FOUNDATION_EXPORT NSString *const kUserDidLoginNotification = @"kUserDidLoginNotification";
static const CGFloat kCornerRadius = 8.0f;
// Swift
let screenWidth = UIScreen.main.bounds.width
let animationDuration: TimeInterval = 0.25
k 只是團隊約定的「常量標識」,編譯器不強制。新項目建議採用語言原生常量聲明,棄用宏定義,獲得類型安全與 IDE 智能提示。