Common Programming Paradigms in Objective‑C: Chainable, Reactive, Functional, Procedural, and Object‑Oriented
This article introduces the major programming paradigms frequently used in Objective‑C—including chainable, reactive, functional, procedural, and object‑oriented styles—explaining their concepts, advantages, and providing concrete code examples to help developers design cleaner, more maintainable iOS applications.
Programming paradigms are high‑level concepts that describe how we use a language to solve real‑world problems; they guide the way we decompose goals, follow specific rules, and combine components into a coherent system.
In iOS development with Objective‑C, several paradigms are commonly encountered. The article first discusses chainable programming , illustrating it with the Masonry layout framework. By chaining method calls with the dot syntax, each call returns the same object (or a block that returns the object), enabling concise, readable code.
self.view addSubView:_testView; [_testView mas_makeConstraints:^(MASConstraintMaker *make) { make.height.equalTo(15).width.equalTo(15); }];
The underlying implementation shows that height returns a MASConstraint object, and equalTo returns a block that finally returns self , allowing further chaining.
- (MASConstraint *)height { return [self addConstraintWithLayoutAttribute:NSLayoutAttributeHeight]; } - (MASConstraint * (^)(id))equalTo { return ^id(id attribute) { return self.equalToWithRelation(attribute, NSLayoutRelationEqual); }; }
Next, the article covers reactive programming . Reactive code focuses on data streams and change propagation, letting developers concentrate on business logic rather than implementation details. In Objective‑C, common reactive mechanisms include NSNotificationCenter , delegates, and KVO. A KVO example demonstrates how a view controller observes a view‑model property and updates the UI automatically.
@interface ViewController () @property (nonatomic, strong) UILabel *showLabel; @property (nonatomic, strong) TestViewModel *viewModel; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [self.viewModel addObserver:self forKeyPath:@"num" options:NSKeyValueObservingOptionOld context:nil]; } - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { self.showLabel.text = [change valueForKey:@"new"]; } @end
The article then explains functional programming , emphasizing first‑class functions, expression‑only code, immutability, and referential transparency. An Objective‑C example shows a manager class that receives a block as a function argument, updates an internal result, and returns self to enable chainable calls.
@interface CaculateManager : NSObject @property (nonatomic, assign) NSInteger result; - (instancetype)caculate:(int (^)(int))caculateBlock; @end @implementation CaculateManager - (instancetype)caculate:(int (^)(int))caculateBlock { _result = caculateBlock(_result); return self; // enables chainable usage } @end int main(int argc, char *argv[]) { @autoreleasepool { CaculateManager *caculateM = [CaculateManager new]; NSInteger result = [caculateM caculate:^int(int result) { result += 100; return result; }].result; } }
Functional ideas also appear in Masonry’s constraint block, where a block receives a MASConstraintMaker and returns an array of constraints.
- (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *))block { block(constraintMaker); return [constraintMaker install]; }
The article briefly touches on procedural programming , describing it as a sequence of ordered steps (functions) that solve a problem, exemplified by the C language.
Finally, it discusses object‑oriented programming , noting that Objective‑C is inherently object‑oriented: classes model real‑world entities, objects are instances, and communication occurs via message passing.
In conclusion, understanding these paradigms helps developers write maintainable, extensible, and elegant Objective‑C code, reducing system complexity, duplication, and change impact.
JD Retail Technology
Official platform of JD Retail Technology, delivering insightful R&D news and a deep look into the lives and work of technologists.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.