Enhanced ObjC Coding Style Cheat Sheet (Version 1.0)
Compact version of the Enhanced ObjC Coding Style, extending the ObjC Coding Style Cheat Sheet and Enhanced C Coding Style Cheat Sheet.
1. Scope
- The standard ObjC Coding Style Cheat Sheet remains authoritative for normal Objective-C formatting and layout.
- The Enhanced C Coding Style Cheat Sheet remains authoritative for the shared wrapper layer.
- This cheat sheet covers only the enhanced Objective-C additions.
2. Activation and Wrapper Rules
- ObjC-only helpers are enabled automatically by
begin_header/end_headerandbegin_impl/end_impl. - Do not include
begin_objc.horend_objc.hdirectly in normal project code. - Do not use the ObjC-only macros in plain C files.
3. Property Attribute Macros
RO=nonatomic,readonly.RW=nonatomic,readwrite.A_RO=atomic,readonly.A_RW=atomic,readwrite.- Prefer
RO/RWby default. Use the atomic forms only intentionally. - Combine them with normal property attributes when needed, for example
@property(RW,weak) ....
4. Class and Member Attribute Macros
final: class cannot be subclassed.private_methods: members use direct Objective-C dispatch.internal: shorthand forfinal private_methods.
5. Direct Method Attribute Macro
direct_call: direct dispatch for one specific method.
6. Disabling Default Initializers
no_default_inits: makesnewandinitunavailable. Use it when designated initializers are required.
7. ObjC Object Guard Macros
- Prefer
guard_objc(...) { ... } endguard,return_unless_objc(...),continue_unless_objc(...), andbreak_unless_objc(...)when binding maybe-nil Objective-C objects. - These macros bind assigned values as nonnull
deflocals after the nil check succeeds. guard_objc(...)supports the same optional trailing condition andelse { ... }form as the sharedguard(...).- The shared
return_unless(...),continue_unless(...), andbreak_unless(...)still remain appropriate for plain conditions and non-object values.
8. ObjC Casting Helper
asOptClass(ClassName, obj)returns a typed object ornil.- Use it with
guard_objc(...)and the other ObjC guard macros for dynamicidinputs.
9. Weak and Strong Capture Helpers
weak(var)createsweak_varbefore a block capture.strong(var)createsstrong_varfromweak_varinside the block.return_unless_strong(returnValue, var)is the common early-exit form when the strong rebound is required before continuing.
10. Preference Rule
- When normal Objective-C syntax and the enhanced ObjC layer are equivalent, prefer the project layer in this project.