Initialization for immutable objects based on the builder pattern.
Objective-C
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
AHKBuilder.xcodeproj
AHKBuilder
AHKBuilderTests
.gitignore
.travis.yml
README.md

README.md

Build Status Carthage compatible

AHKBuilder

AHKBuilder allows you to add initialization based on the builder pattern to your immutable objects with ease. Implementation is described in the blog post: http://holko.pl/2015/05/12/immutable-object-initialization/

Usage

Let's say you have a simple Reminder class:

@interface Reminder : NSObject

@property (nonatomic, copy, readonly) NSString *title;
@property (nonatomic, strong, readonly) NSDate *date;
@property (nonatomic, assign, readonly) BOOL showsAlert;

@end

With just three simple steps you can add ability to your classes to be initialized with the builder block:

  1. Import AHKBuilder with #import <AHKBuilder/AHKBuilder.h>

  2. Add a protocol declaring same properties as your immutable class, but with readwrite modifier, in our case:

@protocol ReminderBuilder <NSObject>

@property (nonatomic, copy, readwrite) NSString *title;
@property (nonatomic, strong, readwrite) NSDate *date;
@property (nonatomic, assign, readwrite) BOOL showsAlert;

@end
  1. Declare initialization and/or copying method using the name of protocol from 1.
@interface Reminder (Builder)

- (instancetype)initWithBuilder_ahk:(void (^)(id<ReminderBuilder> builder))builderBlock;
- (instancetype)copyWithBuilder_ahk:(void (^)(id<ReminderBuilder> builder))builderBlock;

@end

Note: These methods have to be declared in a category, otherwise Xcode will complain that their definitions are missing.

That's all! You can now create instances and copies using these methods, e.g.:

Reminder *reminder = [[Reminder alloc] initWithBuilder_ahk:^(id<ReminderBuilder> builder) {
  builder.title = @"Test reminder";
}];

Requirements

  • iOS 8 and above

Installation

Source files can be found in AHKBuilder folder. AHKBuilder is compatible with Carthage.

Changelog

0.2

Fixed crash when working with objects conforming to protocols inheriting from NSObject.

0.1

Initial release.

Author

Arkadiusz Holko: