Skip to content

Commit 66dc4be

Browse files
author
Sergey Minakov
committed
switch for type casting. guard as precondition.
1 parent cc8f43a commit 66dc4be

14 files changed

+87
-90
lines changed

Foundation/Bridging.swift

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -121,20 +121,18 @@ internal final class _SwiftValue : NSObject, NSCopying {
121121
}
122122

123123
override func isEqual(_ value: Any?) -> Bool {
124-
if let other = value as? _SwiftValue {
125-
if self === other {
126-
return true
127-
}
128-
if let otherHashable = other.value as? AnyHashable,
129-
let hashable = self.value as? AnyHashable {
130-
return otherHashable == hashable
131-
}
124+
switch value {
125+
case let other as _SwiftValue:
126+
guard let left = other.value as? AnyHashable,
127+
let right = self.value as? AnyHashable else { return self === other }
132128

133-
} else if let otherHashable = value as? AnyHashable,
134-
let hashable = self.value as? AnyHashable {
135-
return otherHashable == hashable
129+
return left == right
130+
case let other as AnyHashable:
131+
guard let hashable = self.value as? AnyHashable else { return false }
132+
return other == hashable
133+
default:
134+
return false
136135
}
137-
return false
138136
}
139137

140138
public func copy(with zone: NSZone?) -> Any {

Foundation/NSArray.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,14 @@ open class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCo
137137
}
138138

139139
open override func isEqual(_ value: Any?) -> Bool {
140-
if let other = value as? [Any] {
140+
switch value {
141+
case let other as [Any]:
141142
return self.isEqual(to: other)
142-
} else if let other = value as? NSArray {
143+
case let other as NSArray:
143144
return self.isEqual(to: other.allObjects)
145+
default:
146+
return false
144147
}
145-
return false
146148
}
147149

148150
open override var hash: Int {

Foundation/NSCalendar.swift

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -225,15 +225,14 @@ open class NSCalendar : NSObject, NSCopying, NSSecureCoding {
225225
}
226226

227227
open override func isEqual(_ value: Any?) -> Bool {
228-
if let cal = value as? Calendar {
229-
return CFEqual(_cfObject, cal._cfObject)
230-
} else if let cal = value as? NSCalendar {
231-
if cal === self {
232-
return true
233-
}
234-
return CFEqual(_cfObject, cal._cfObject)
228+
switch value {
229+
case let other as Calendar:
230+
return CFEqual(_cfObject, other._cfObject)
231+
case let other as NSCalendar:
232+
return other === self || CFEqual(_cfObject, other._cfObject)
233+
default:
234+
return false
235235
}
236-
return false
237236
}
238237

239238
open override var description: String {

Foundation/NSCharacterSet.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,14 @@ open class NSCharacterSet : NSObject, NSCopying, NSMutableCopying, NSCoding {
5050
}
5151

5252
open override func isEqual(_ value: Any?) -> Bool {
53-
if let cs = value as? CharacterSet {
54-
return CFEqual(_cfObject, cs._cfObject)
55-
} else if let cs = value as? NSCharacterSet {
56-
return CFEqual(_cfObject, cs._cfObject)
53+
switch value {
54+
case let other as CharacterSet:
55+
return CFEqual(_cfObject, other._cfObject)
56+
case let other as NSCharacterSet:
57+
return CFEqual(_cfObject, other._cfObject)
58+
default:
59+
return false
5760
}
58-
return false
5961
}
6062

6163
open override var description: String {

Foundation/NSConcreteValue.swift

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -167,12 +167,8 @@ internal class NSConcreteValue : NSValue {
167167
}
168168

169169
override func isEqual(_ value: Any?) -> Bool {
170-
if let other = value as? NSConcreteValue {
171-
return self._typeInfo == other._typeInfo &&
172-
self._isEqualToValue(other)
173-
} else {
174-
return false
175-
}
170+
guard let other = value as? NSConcreteValue else { return false }
171+
return self._typeInfo == other._typeInfo && self._isEqualToValue(other)
176172
}
177173

178174
override var hash: Int {

Foundation/NSDate.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,14 @@ open class NSDate : NSObject, NSCopying, NSSecureCoding, NSCoding {
2929
}
3030

3131
open override func isEqual(_ value: Any?) -> Bool {
32-
if let date = value as? Date {
33-
return isEqual(to: date)
34-
} else if let date = value as? NSDate {
35-
return isEqual(to: Date(timeIntervalSinceReferenceDate: date.timeIntervalSinceReferenceDate))
32+
switch value {
33+
case let other as Date:
34+
return isEqual(to: other)
35+
case let other as NSDate:
36+
return isEqual(to: Date(timeIntervalSinceReferenceDate: other.timeIntervalSinceReferenceDate))
37+
default:
38+
return false
3639
}
37-
return false
3840
}
3941

4042
deinit {

Foundation/NSDecimalNumber.swift

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -341,11 +341,8 @@ open class NSDecimalNumber : NSNumber {
341341
}
342342

343343
open override func isEqual(_ value: Any?) -> Bool {
344-
if let number = value as? NSDecimalNumber {
345-
return self.decimal == number.decimal
346-
} else {
347-
return false
348-
}
344+
guard let other = value as? NSDecimalNumber else { return false }
345+
return self.decimal == other.decimal
349346
}
350347

351348
}

Foundation/NSDictionary.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,14 @@ open class NSDictionary : NSObject, NSCopying, NSMutableCopying, NSSecureCoding,
142142
}
143143

144144
open override func isEqual(_ value: Any?) -> Bool {
145-
if let other = value as? Dictionary<AnyHashable, Any> {
145+
switch value {
146+
case let other as Dictionary<AnyHashable, Any>:
146147
return isEqual(to: other)
147-
} else if let other = value as? NSDictionary {
148+
case let other as NSDictionary:
148149
return isEqual(to: Dictionary._unconditionallyBridgeFromObjectiveC(other))
150+
default:
151+
return false
149152
}
150-
return false
151153
}
152154

153155
open override var hash: Int {

Foundation/NSNumber.swift

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -212,16 +212,18 @@ open class NSNumber : NSValue {
212212
}
213213

214214
open override func isEqual(_ value: Any?) -> Bool {
215-
if let number = value as? Int {
216-
return intValue == number
217-
} else if let number = value as? Double {
218-
return doubleValue == number
219-
} else if let number = value as? Bool {
220-
return boolValue == number
221-
} else if let number = value as? NSNumber {
222-
return CFEqual(_cfObject, number._cfObject)
215+
switch value {
216+
case let other as Int:
217+
return intValue == other
218+
case let other as Double:
219+
return doubleValue == other
220+
case let other as Bool:
221+
return boolValue == other
222+
case let other as NSNumber:
223+
return CFEqual(_cfObject, other._cfObject)
224+
default:
225+
return false
223226
}
224-
return false
225227
}
226228

227229
open override var objCType: UnsafePointer<Int8> {

Foundation/NSSet.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,14 @@ open class NSSet : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCodi
114114
}
115115

116116
open override func isEqual(_ value: Any?) -> Bool {
117-
if let other = value as? Set<AnyHashable> {
117+
switch value {
118+
case let other as Set<AnyHashable>:
118119
return isEqual(to: other)
119-
} else if let other = value as? NSSet {
120+
case let other as NSSet:
120121
return isEqual(to: Set._unconditionallyBridgeFromObjectiveC(other))
122+
default:
123+
return false
121124
}
122-
return false
123125
}
124126

125127
open override var hash: Int {

Foundation/NSSpecialValue.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -134,14 +134,14 @@ internal class NSSpecialValue : NSValue {
134134
}
135135

136136
override func isEqual(_ value: Any?) -> Bool {
137-
if let object = value as? NSObject {
138-
if self === object {
139-
return true
140-
} else if let special = object as? NSSpecialValue {
141-
return _value.isEqual(special._value)
142-
}
137+
switch value {
138+
case let other as NSSpecialValue:
139+
return _value.isEqual(other._value)
140+
case let other as NSObject:
141+
return self === other
142+
default:
143+
return false
143144
}
144-
return false
145145
}
146146

147147
override var hash: Int {

Foundation/NSSwiftRuntime.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,8 @@ internal class __NSCFType : NSObject {
3939
}
4040

4141
override func isEqual(_ value: Any?) -> Bool {
42-
if let other = value as? NSObject {
43-
return CFEqual(self, other)
44-
}
45-
return false
42+
guard let other = value as? NSObject else { return false }
43+
return CFEqual(self, other)
4644
}
4745

4846
override var description: String {

Foundation/NSUUID.swift

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ open class NSUUID : NSObject, NSCopying, NSSecureCoding, NSCoding {
7878
}
7979

8080
open override func isEqual(_ value: Any?) -> Bool {
81-
if let other = value as? UUID {
81+
switch value {
82+
case let other as UUID:
8283
return other.uuid.0 == buffer[0] &&
8384
other.uuid.1 == buffer[1] &&
8485
other.uuid.2 == buffer[2] &&
@@ -95,13 +96,11 @@ open class NSUUID : NSObject, NSCopying, NSSecureCoding, NSCoding {
9596
other.uuid.13 == buffer[13] &&
9697
other.uuid.14 == buffer[14] &&
9798
other.uuid.15 == buffer[15]
98-
} else if let other = value as? NSUUID {
99-
if other === self {
100-
return true
101-
}
102-
return _cf_uuid_compare(buffer, other.buffer) == 0
99+
case let other as NSUUID:
100+
return other === self || _cf_uuid_compare(buffer, other.buffer) == 0
101+
default:
102+
return false
103103
}
104-
return false
105104
}
106105

107106
open override var hash: Int {

Foundation/NSValue.swift

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,21 +52,19 @@ open class NSValue : NSObject, NSCopying, NSSecureCoding, NSCoding {
5252
}
5353

5454
open override func isEqual(_ value: Any?) -> Bool {
55-
if let object = value as? NSValue {
56-
if self === object {
57-
return true
58-
} else {
59-
// bypass _concreteValue accessor in order to avoid acquiring lock twice
60-
let (lhs, rhs) = NSValue.SideTableLock.synchronized {
61-
return (NSValue.SideTable[ObjectIdentifier(self)],
62-
NSValue.SideTable[ObjectIdentifier(object)])
63-
}
64-
if let lhs = lhs, let rhs = rhs {
65-
return lhs.isEqual(rhs)
66-
}
55+
guard let object = value as? NSValue else { return false }
56+
57+
if self === object {
58+
return true
59+
} else {
60+
// bypass _concreteValue accessor in order to avoid acquiring lock twice
61+
let (lhs, rhs) = NSValue.SideTableLock.synchronized {
62+
return (NSValue.SideTable[ObjectIdentifier(self)],
63+
NSValue.SideTable[ObjectIdentifier(object)])
6764
}
65+
guard let left = lhs, let right = rhs else { return false }
66+
return left.isEqual(right)
6867
}
69-
return false
7068
}
7169

7270
open override var description : String {

0 commit comments

Comments
 (0)