Skip to content

Commit 3cde50a

Browse files
koukyparkera
authored andcommitted
Guard clauses for NSRequiresConcreteImplementation (#376)
1 parent 753e755 commit 3cde50a

File tree

6 files changed

+99
-125
lines changed

6 files changed

+99
-125
lines changed

Foundation/NSArray.swift

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,17 @@ public class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NS
3939
internal var _storage = [AnyObject]()
4040

4141
public var count: Int {
42-
if self.dynamicType === NSArray.self || self.dynamicType === NSMutableArray.self {
43-
return _storage.count
44-
} else {
42+
guard self.dynamicType === NSArray.self || self.dynamicType === NSMutableArray.self else {
4543
NSRequiresConcreteImplementation()
4644
}
45+
return _storage.count
4746
}
4847

4948
public func objectAtIndex(_ index: Int) -> AnyObject {
50-
if self.dynamicType === NSArray.self || self.dynamicType === NSMutableArray.self {
51-
return _storage[index]
52-
} else {
53-
NSRequiresConcreteImplementation()
49+
guard self.dynamicType === NSArray.self || self.dynamicType === NSMutableArray.self else {
50+
NSRequiresConcreteImplementation()
5451
}
52+
return _storage[index]
5553
}
5654

5755
public convenience override init() {
@@ -617,11 +615,10 @@ public class NSMutableArray : NSArray {
617615
}
618616

619617
public func insertObject(_ anObject: AnyObject, atIndex index: Int) {
620-
if self.dynamicType === NSMutableArray.self {
621-
_storage.insert(anObject, at: index)
622-
} else {
618+
guard self.dynamicType === NSMutableArray.self else {
623619
NSRequiresConcreteImplementation()
624620
}
621+
_storage.insert(anObject, at: index)
625622
}
626623

627624
public func removeLastObject() {
@@ -631,21 +628,19 @@ public class NSMutableArray : NSArray {
631628
}
632629

633630
public func removeObjectAtIndex(_ index: Int) {
634-
if self.dynamicType === NSMutableArray.self {
635-
_storage.remove(at: index)
636-
} else {
631+
guard self.dynamicType === NSMutableArray.self else {
637632
NSRequiresConcreteImplementation()
638633
}
634+
_storage.remove(at: index)
639635
}
640636

641637
public func replaceObjectAtIndex(_ index: Int, withObject anObject: AnyObject) {
642-
if self.dynamicType === NSMutableArray.self {
643-
let min = index
644-
let max = index + 1
645-
_storage.replaceSubrange(min..<max, with: [anObject])
646-
} else {
638+
guard self.dynamicType === NSMutableArray.self else {
647639
NSRequiresConcreteImplementation()
648640
}
641+
let min = index
642+
let max = index + 1
643+
_storage.replaceSubrange(min..<max, with: [anObject])
649644
}
650645

651646
public convenience init() {

Foundation/NSDictionary.swift

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -87,27 +87,24 @@ public class NSDictionary : NSObject, NSCopying, NSMutableCopying, NSSecureCodin
8787
internal var _storage = [NSObject: AnyObject]()
8888

8989
public var count: Int {
90-
if self.dynamicType === NSDictionary.self || self.dynamicType === NSMutableDictionary.self {
91-
return _storage.count
92-
} else {
90+
guard self.dynamicType === NSDictionary.self || self.dynamicType === NSMutableDictionary.self else {
9391
NSRequiresConcreteImplementation()
9492
}
93+
return _storage.count
9594
}
9695

9796
public func objectForKey(_ aKey: AnyObject) -> AnyObject? {
98-
if self.dynamicType === NSDictionary.self || self.dynamicType === NSMutableDictionary.self {
99-
return _storage[aKey as! NSObject]
100-
} else {
97+
guard self.dynamicType === NSDictionary.self || self.dynamicType === NSMutableDictionary.self else {
10198
NSRequiresConcreteImplementation()
10299
}
100+
return _storage[aKey as! NSObject]
103101
}
104102

105103
public func keyEnumerator() -> NSEnumerator {
106-
if self.dynamicType === NSDictionary.self || self.dynamicType === NSMutableDictionary.self {
107-
return NSGeneratorEnumerator(_storage.keys.makeIterator())
108-
} else {
104+
guard self.dynamicType === NSDictionary.self || self.dynamicType === NSMutableDictionary.self else {
109105
NSRequiresConcreteImplementation()
110106
}
107+
return NSGeneratorEnumerator(_storage.keys.makeIterator())
111108
}
112109

113110
public override convenience init() {
@@ -567,24 +564,20 @@ extension Dictionary : _NSBridgable, _CFBridgable {
567564
public class NSMutableDictionary : NSDictionary {
568565

569566
public func removeObjectForKey(_ aKey: AnyObject) {
570-
if self.dynamicType === NSDictionary.self || self.dynamicType === NSMutableDictionary.self {
571-
if let key = aKey as? NSObject {
572-
_storage.removeValue(forKey: key)
573-
}
574-
575-
// CFDictionaryRemoveValue(unsafeBitCast(self, CFMutableDictionaryRef.self), unsafeBitCast(aKey, UnsafePointer<Void>.self))
576-
} else {
567+
guard self.dynamicType === NSDictionary.self || self.dynamicType === NSMutableDictionary.self else {
577568
NSRequiresConcreteImplementation()
578569
}
570+
571+
if let key = aKey as? NSObject {
572+
_storage.removeValue(forKey: key)
573+
}
579574
}
580575

581576
public func setObject(_ anObject: AnyObject, forKey aKey: NSObject) {
582-
if self.dynamicType === NSDictionary.self || self.dynamicType === NSMutableDictionary.self {
583-
_storage[aKey] = anObject
584-
// CFDictionarySetValue(unsafeBitCast(self, CFMutableDictionaryRef.self), unsafeBitCast(aKey, UnsafePointer<Void>.self), unsafeBitCast(anObject, UnsafePointer<Void>.self))
585-
} else {
577+
guard self.dynamicType === NSDictionary.self || self.dynamicType === NSMutableDictionary.self else {
586578
NSRequiresConcreteImplementation()
587579
}
580+
_storage[aKey] = anObject
588581
}
589582

590583
public convenience required init() {

Foundation/NSOrderedSet.swift

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -217,19 +217,17 @@ extension NSOrderedSet {
217217
}
218218

219219
public func objectEnumerator() -> NSEnumerator {
220-
if self.dynamicType === NSOrderedSet.self || self.dynamicType === NSMutableOrderedSet.self {
221-
return NSGeneratorEnumerator(_orderedStorage.makeIterator())
222-
} else {
220+
guard self.dynamicType === NSOrderedSet.self || self.dynamicType === NSMutableOrderedSet.self else {
223221
NSRequiresConcreteImplementation()
224222
}
223+
return NSGeneratorEnumerator(_orderedStorage.makeIterator())
225224
}
226225

227226
public func reverseObjectEnumerator() -> NSEnumerator {
228-
if self.dynamicType === NSOrderedSet.self || self.dynamicType === NSMutableOrderedSet.self {
229-
return NSGeneratorEnumerator(_orderedStorage.reversed().makeIterator())
230-
} else {
227+
guard self.dynamicType === NSOrderedSet.self || self.dynamicType === NSMutableOrderedSet.self else {
231228
NSRequiresConcreteImplementation()
232229
}
230+
return NSGeneratorEnumerator(_orderedStorage.reversed().makeIterator())
233231
}
234232

235233
/*@NSCopying*/

Foundation/NSSet.swift

Lines changed: 43 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -75,30 +75,29 @@ public class NSSet : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCo
7575
internal var _storage: Set<NSObject>
7676

7777
public var count: Int {
78-
if self.dynamicType === NSSet.self || self.dynamicType === NSMutableSet.self || self.dynamicType === NSCountedSet.self {
79-
return _storage.count
80-
} else {
81-
NSRequiresConcreteImplementation()
78+
guard self.dynamicType === NSSet.self || self.dynamicType === NSMutableSet.self || self.dynamicType === NSCountedSet.self else {
79+
NSRequiresConcreteImplementation()
8280
}
81+
return _storage.count
8382
}
8483

8584
public func member(_ object: AnyObject) -> AnyObject? {
86-
if self.dynamicType === NSSet.self || self.dynamicType === NSMutableSet.self || self.dynamicType === NSCountedSet.self {
87-
if let obj = object as? NSObject where _storage.contains(obj) {
88-
return obj // this is not exactly the same behavior, but it is reasonably close
89-
}
90-
return nil
91-
} else {
85+
guard self.dynamicType === NSSet.self || self.dynamicType === NSMutableSet.self || self.dynamicType === NSCountedSet.self else {
9286
NSRequiresConcreteImplementation()
9387
}
88+
89+
guard let obj = object as? NSObject where _storage.contains(obj) else {
90+
return nil
91+
}
92+
93+
return obj // this is not exactly the same behavior, but it is reasonably close
9494
}
9595

9696
public func objectEnumerator() -> NSEnumerator {
97-
if self.dynamicType === NSSet.self || self.dynamicType === NSMutableSet.self || self.dynamicType === NSCountedSet.self {
98-
return NSGeneratorEnumerator(_storage.makeIterator())
99-
} else {
97+
guard self.dynamicType === NSSet.self || self.dynamicType === NSMutableSet.self || self.dynamicType === NSCountedSet.self else {
10098
NSRequiresConcreteImplementation()
10199
}
100+
return NSGeneratorEnumerator(_storage.makeIterator())
102101
}
103102

104103
public convenience override init() {
@@ -355,21 +354,20 @@ extension NSSet : Sequence {
355354
public class NSMutableSet : NSSet {
356355

357356
public func addObject(_ object: AnyObject) {
358-
if self.dynamicType === NSMutableSet.self {
359-
_storage.insert(object as! NSObject)
360-
} else {
357+
guard self.dynamicType === NSMutableSet.self else {
361358
NSRequiresConcreteImplementation()
362359
}
360+
_storage.insert(object as! NSObject)
363361
}
364362

365363
public func removeObject(_ object: AnyObject) {
366-
if self.dynamicType === NSMutableSet.self {
367-
if let obj = object as? NSObject {
368-
_storage.remove(obj)
369-
}
370-
} else {
364+
guard self.dynamicType === NSMutableSet.self else {
371365
NSRequiresConcreteImplementation()
372366
}
367+
368+
if let obj = object as? NSObject {
369+
_storage.remove(obj)
370+
}
373371
}
374372

375373
override public init(objects: UnsafePointer<AnyObject?>, count cnt: Int) {
@@ -497,43 +495,42 @@ public class NSCountedSet : NSMutableSet {
497495
}
498496

499497
public func countForObject(_ object: AnyObject) -> Int {
500-
if self.dynamicType === NSCountedSet.self {
501-
guard let count = _table[object as! NSObject] else {
502-
return 0
503-
}
504-
return count
505-
} else {
498+
guard self.dynamicType === NSCountedSet.self else {
506499
NSRequiresConcreteImplementation()
507500
}
501+
guard let count = _table[object as! NSObject] else {
502+
return 0
503+
}
504+
return count
508505
}
509506

510507
public override func addObject(_ object: AnyObject) {
511-
if self.dynamicType === NSCountedSet.self {
512-
if let count = _table[object as! NSObject] {
513-
_table[object as! NSObject] = count + 1
514-
} else {
515-
_table[object as! NSObject] = 1
516-
_storage.insert(object as! NSObject)
517-
}
518-
} else {
508+
guard self.dynamicType === NSCountedSet.self else {
519509
NSRequiresConcreteImplementation()
520510
}
511+
512+
if let count = _table[object as! NSObject] {
513+
_table[object as! NSObject] = count + 1
514+
} else {
515+
_table[object as! NSObject] = 1
516+
_storage.insert(object as! NSObject)
517+
}
521518
}
522519

523520
public override func removeObject(_ object: AnyObject) {
524-
if self.dynamicType === NSCountedSet.self {
525-
guard let count = _table[object as! NSObject] else {
526-
return
527-
}
528-
if count > 1 {
529-
_table[object as! NSObject] = count - 1
530-
} else {
531-
_table[object as! NSObject] = nil
532-
_storage.remove(object as! NSObject)
533-
}
534-
} else {
521+
guard self.dynamicType === NSCountedSet.self else {
535522
NSRequiresConcreteImplementation()
536523
}
524+
guard let count = _table[object as! NSObject] else {
525+
return
526+
}
527+
528+
if count > 1 {
529+
_table[object as! NSObject] = count - 1
530+
} else {
531+
_table[object as! NSObject] = nil
532+
_storage.remove(object as! NSObject)
533+
}
537534
}
538535

539536
public override func removeAllObjects() {

Foundation/NSString.swift

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -210,20 +210,18 @@ public class NSString : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, N
210210
internal var _storage: String
211211

212212
public var length: Int {
213-
if self.dynamicType === NSString.self || self.dynamicType === NSMutableString.self {
214-
return _storage.utf16.count
215-
} else {
213+
guard self.dynamicType === NSString.self || self.dynamicType === NSMutableString.self else {
216214
NSRequiresConcreteImplementation()
217215
}
216+
return _storage.utf16.count
218217
}
219218

220219
public func character(at index: Int) -> unichar {
221-
if self.dynamicType === NSString.self || self.dynamicType === NSMutableString.self {
222-
let start = _storage.utf16.startIndex
223-
return _storage.utf16[start.advanced(by: index)]
224-
} else {
220+
guard self.dynamicType === NSString.self || self.dynamicType === NSMutableString.self else {
225221
NSRequiresConcreteImplementation()
226222
}
223+
let start = _storage.utf16.startIndex
224+
return _storage.utf16[start.advanced(by: index)]
227225
}
228226

229227
public override convenience init() {
@@ -1291,15 +1289,15 @@ extension NSString : StringLiteralConvertible { }
12911289

12921290
public class NSMutableString : NSString {
12931291
public func replaceCharacters(in range: NSRange, with aString: String) {
1294-
if self.dynamicType === NSString.self || self.dynamicType === NSMutableString.self {
1295-
// this is incorrectly calculated for grapheme clusters that have a size greater than a single unichar
1296-
let start = _storage.startIndex
1297-
let min = _storage.index(start, offsetBy: range.location)
1298-
let max = _storage.index(start, offsetBy: range.location + range.length)
1299-
_storage.replaceSubrange(min..<max, with: aString)
1300-
} else {
1292+
guard self.dynamicType === NSString.self || self.dynamicType === NSMutableString.self else {
13011293
NSRequiresConcreteImplementation()
13021294
}
1295+
1296+
// this is incorrectly calculated for grapheme clusters that have a size greater than a single unichar
1297+
let start = _storage.startIndex
1298+
let min = _storage.index(start, offsetBy: range.location)
1299+
let max = _storage.index(start, offsetBy: range.location + range.length)
1300+
_storage.replaceSubrange(min..<max, with: aString)
13031301
}
13041302

13051303
public required override init(characters: UnsafePointer<unichar>, length: Int) {

0 commit comments

Comments
 (0)