From d20efeceadde55dd0e7a6c2b4ab22a2d7a76b347 Mon Sep 17 00:00:00 2001 From: Gregory John Casamento Date: Mon, 17 Jun 2019 11:57:18 -0400 Subject: [PATCH] Add check for nil. Force use of subclass init methods --- Source/GSOrderedSet.m | 19 +++++++++++++------ Source/NSOrderedSet.m | 24 +++++++----------------- 2 files changed, 20 insertions(+), 23 deletions(-) diff --git a/Source/GSOrderedSet.m b/Source/GSOrderedSet.m index b14568ccd..3b6c3cb94 100644 --- a/Source/GSOrderedSet.m +++ b/Source/GSOrderedSet.m @@ -161,7 +161,7 @@ static Class mutableSetClass; return [self count]; } -- (id) init +- (instancetype) init { return [self initWithObjects: NULL count: 0]; } @@ -280,12 +280,19 @@ static Class mutableSetClass; - (void) insertObject: (id)object atIndex: (NSUInteger)index { GSIArrayItem item; - if([self containsObject: object] == NO) + if(object == nil) { - item.obj = object; - GSIArrayInsertItem(&array, item, index); - RETAIN(object); - _version++; + NSWarnLog(@"Attempt to insert nil object"); + } + else + { + if([self containsObject: object] == NO) + { + item.obj = object; + GSIArrayInsertItem(&array, item, index); + RETAIN(object); + _version++; + } } } diff --git a/Source/NSOrderedSet.m b/Source/NSOrderedSet.m index c287f0957..c225b743d 100644 --- a/Source/NSOrderedSet.m +++ b/Source/NSOrderedSet.m @@ -506,12 +506,8 @@ static SEL rlSel; - (instancetype) initWithObjects:(const id [])objects // required override. count:(NSUInteger)count { - self = [self init]; - if(self != nil) - { - // Need proper implementation in subclass since that is where data will be stored. - } - return self; + [self subclassResponsibility: _cmd]; + return nil; } - (instancetype) initWithOrderedSet:(NSOrderedSet *)aSet @@ -622,12 +618,8 @@ static SEL rlSel; - (instancetype) init { - self = [super init]; - if(self == nil) - { - NSLog(@"NSOrderedSet not allocated."); - } - return self; + [self subclassResponsibility: _cmd]; + return nil; } - (NSUInteger) count // required override @@ -1068,13 +1060,11 @@ static SEL rlSel; // Key value coding support - (void) setValue: (id)value forKey: (NSString*)key { - NSUInteger i; - NSUInteger count = [self count]; volatile id object = nil; - - for (i = 0; i < count; i++) + NSEnumerator *e = [self objectEnumerator]; + + while((object = [e nextObject]) != nil) { - object = [self objectAtIndex: i]; [object setValue: value forKey: key]; }