Skip to content

Commit 3d7cd4b

Browse files
author
Richard Piazza
committed
Default Injection
Added setDefaults() when working with swift so all initializers do not need to be overridden.
1 parent c9f361f commit 3d7cd4b

File tree

6 files changed

+50
-9
lines changed

6 files changed

+50
-9
lines changed

CodeQuickKit-ObjC/CQKSerializableNSManagedObject.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,7 @@
4040
/*! @abstract Calls the default initializer then passes the referenced dictionary to `CQKSerializable` updateWithJSON:. */
4141
- (nullable instancetype)initIntoManagedObjectContext:(nonnull NSManagedObjectContext *)context withJSON:(nullable NSString *)json;
4242

43+
/// Allows for injection of default values during designated initialization.
44+
- (void)setDefaults;
45+
4346
@end

CodeQuickKit-ObjC/CQKSerializableNSManagedObject.m

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ - (void)setValueForPropertyName:(nullable NSString *)propertyName withObject:(nu
3636

3737
@implementation CQKSerializableNSManagedObject
3838

39+
- (instancetype)initWithEntity:(NSEntityDescription *)entity insertIntoManagedObjectContext:(NSManagedObjectContext *)context
40+
{
41+
self = [super initWithEntity:entity insertIntoManagedObjectContext:context];
42+
if (self != nil) {
43+
[self setDefaults];
44+
}
45+
return self;
46+
}
47+
3948
- (instancetype)initIntoManagedObjectContext:(NSManagedObjectContext *)context
4049
{
4150
NSString *entityName = self.classNameWithoutModule;
@@ -77,6 +86,11 @@ - (instancetype)initIntoManagedObjectContext:(NSManagedObjectContext *)context w
7786
return self;
7887
}
7988

89+
- (void)setDefaults
90+
{
91+
92+
}
93+
8094
#pragma mark - CQKSerializable -
8195
- (instancetype)initWithDictionary:(NSDictionary<NSString *,__kindof NSObject *> *)dictionary
8296
{

CodeQuickKit-ObjC/CQKSerializableNSObject.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,7 @@
3030
/// objects to a dictionary/json string.
3131
@interface CQKSerializableNSObject : NSObject <NSCoding, NSCopying, CQKSerializable, CQKSerializableCustomizable>
3232

33+
/// Allows for injection of default values during designated initialization.
34+
- (void)setDefaults;
35+
3336
@end

CodeQuickKit-ObjC/CQKSerializableNSObject.m

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,22 @@ - (void)setValueForPropertyName:(NSString *)propertyName withObject:(id)object;
3838

3939
@implementation CQKSerializableNSObject
4040

41+
- (instancetype)init
42+
{
43+
self = [super init];
44+
if (self != nil) {
45+
[self setDefaults];
46+
}
47+
return self;
48+
}
49+
50+
- (void)setDefaults
51+
{
52+
53+
}
54+
4155
#pragma mark - NSCoding -
42-
- (id)initWithCoder:(NSCoder *)decoder
56+
- (instancetype)initWithCoder:(NSCoder *)decoder
4357
{
4458
self = [self init];
4559
if (self != nil) {
@@ -88,7 +102,7 @@ - (id)copyWithZone:(NSZone *)zone
88102
}
89103

90104
#pragma mark - CQKSerializable -
91-
- (nonnull instancetype)initWithDictionary:(NSDictionary<NSString *,__kindof NSObject *> *)dictionary
105+
- (instancetype)initWithDictionary:(NSDictionary<NSString *,__kindof NSObject *> *)dictionary
92106
{
93107
self = [self init];
94108
if (self != nil) {
@@ -171,7 +185,7 @@ - (void)updateWithDictionary:(NSDictionary<NSString *,__kindof NSObject *> *)dic
171185
return dictionary;
172186
}
173187

174-
- (nonnull instancetype)initWithData:(nullable NSData *)data;
188+
- (instancetype)initWithData:(NSData *)data;
175189
{
176190
self = [self init];
177191
if (self != nil) {
@@ -180,7 +194,7 @@ - (nonnull instancetype)initWithData:(nullable NSData *)data;
180194
return self;
181195
}
182196

183-
- (void)updateWithData:(nullable NSData *)data
197+
- (void)updateWithData:(NSData *)data
184198
{
185199
if (data == nil) {
186200
return;
@@ -197,7 +211,7 @@ - (void)updateWithData:(nullable NSData *)data
197211
[self updateWithDictionary:dictionary];
198212
}
199213

200-
- (nullable NSData *)data
214+
- (NSData *)data
201215
{
202216
NSDictionary *dictionary = self.dictionary;
203217

@@ -220,7 +234,7 @@ - (nullable NSData *)data
220234
}
221235
}
222236

223-
- (nonnull instancetype)initWithJSON:(nullable NSString *)json
237+
- (instancetype)initWithJSON:(NSString *)json
224238
{
225239
self = [self init];
226240
if (self != nil) {
@@ -229,7 +243,7 @@ - (nonnull instancetype)initWithJSON:(nullable NSString *)json
229243
return self;
230244
}
231245

232-
- (void)updateWithJSON:(nullable NSString *)json
246+
- (void)updateWithJSON:(NSString *)json
233247
{
234248
if (json == nil || [json isEqualToString:@""]) {
235249
[CQKLogger log:CQKLoggerLevelWarn message:@"Could not updateWithJson:, json is nil." error:nil callingClass:self.class];
@@ -245,7 +259,7 @@ - (void)updateWithJSON:(nullable NSString *)json
245259
[self updateWithData:data];
246260
}
247261

248-
- (nullable NSString *)json
262+
- (NSString *)json
249263
{
250264
NSData *data = self.data;
251265
if (data == nil) {

CodeQuickKit.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
Pod::Spec.new do |s|
1010
s.name = "CodeQuickKit"
11-
s.version = "1.1.3"
11+
s.version = "1.1.4"
1212
s.summary = "An iOS Library simplifying some everyday tasks."
1313
s.description = <<-DESC
1414
CodeQuickKit is a collection of Objective-C Categories and helper classes designed to

CodeQuickKit/CodeQuickKitTests/CQKCoreDataTests.m

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ @implementation CQKCDPerson
6363
@dynamic name;
6464
@dynamic addresses;
6565

66+
- (void)setDefaults
67+
{
68+
[super setDefaults];
69+
70+
[self setName:@"Unknown"];
71+
}
72+
6673
- (Class)objectClassOfCollectionTypeForPropertyName:(NSString *)propertyName
6774
{
6875
if ([propertyName isEqualToString:@"addresses"]) {

0 commit comments

Comments
 (0)