Item 9: Use the Class Cluster Pattern to Hide Implementation Detail
Basic idea is having subclasses all inherit from the same superclass, and have the superclass return the subclass as itself
Keeps code from having a bunch of conditionals, and hides implementation details of the subclasses.
Example is UIButton, has multiple types, but always returns as a UIButton when initialized (buttonWithType:)
Any methods that need to be public, would be defined in base class and overridden.
No way to designate base class as abstract, use code documentation and do not provide initializers.
NSArray (and most collection types) are class clusters, that’s how NSArray and NSMutableArray share code (all immutable methods, shared by both, are defined in the abstract class).
Put protocols in their own file so they can be imported separately in other header files, except delegate protocols because delegates can be implemented in class continuation category (in implementation file).
Always prefer conforming to protocol in implementation.
Item 3: Prefer Literal Syntax over the Equivalent Methods
Prefer use for strings, numbers, arrays and dictionaries
Mutable array can be written [@[@1, @2, @3, @4] mutable copy]
Item 4: Prefer Typed Constants to Preprocessor #define
Avoid preprocessor #define – lose type safety and can accidentally redeclare something with no compiler error.
static (for constants) tells compiler variable is “local to the translation unit” which is a fancy way of saying to the implementation file. Use this to avoid duplicate symbols when compiling (static and const means compiler doesn’t create a symbol at all, replaces occurrences just like #define).
extern in header file allows compiler to know there’s a constant with that name. Without it things won’t compile, but technically symbol is still created when const int = 1 is added to implementation file.