Skip to content

Commit adbc558

Browse files
author
Sergey Minakov
committed
more guard for 'isEqual(...)'
1 parent 66dc4be commit adbc558

10 files changed

+74
-154
lines changed

Foundation/NSAffineTransform.swift

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -334,12 +334,9 @@ open class NSAffineTransform : NSObject, NSCopying, NSSecureCoding {
334334
}
335335

336336
open override func isEqual(_ object: Any?) -> Bool {
337-
if let other = object as? NSAffineTransform {
338-
return other === self
339-
|| (other.transformStruct == self.transformStruct)
340-
}
341-
342-
return false
337+
guard let other = object as? NSAffineTransform else { return false }
338+
return other === self
339+
|| (other.transformStruct == self.transformStruct)
343340
}
344341

345342
public static var supportsSecureCoding: Bool {

Foundation/NSCalendar.swift

Lines changed: 20 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1313,61 +1313,26 @@ open class NSDateComponents : NSObject, NSCopying, NSSecureCoding {
13131313
}
13141314

13151315
open override func isEqual(_ object: Any?) -> Bool {
1316-
if let other = object as? NSDateComponents {
1317-
if era != other.era {
1318-
return false
1319-
}
1320-
if year != other.year {
1321-
return false
1322-
}
1323-
if quarter != other.quarter {
1324-
return false
1325-
}
1326-
if month != other.month {
1327-
return false
1328-
}
1329-
if day != other.day {
1330-
return false
1331-
}
1332-
if hour != other.hour {
1333-
return false
1334-
}
1335-
if minute != other.minute {
1336-
return false
1337-
}
1338-
if second != other.second {
1339-
return false
1340-
}
1341-
if nanosecond != other.nanosecond {
1342-
return false
1343-
}
1344-
if weekOfYear != other.weekOfYear {
1345-
return false
1346-
}
1347-
if weekOfMonth != other.weekOfMonth {
1348-
return false
1349-
}
1350-
if yearForWeekOfYear != other.yearForWeekOfYear {
1351-
return false
1352-
}
1353-
if weekday != other.weekday {
1354-
return false
1355-
}
1356-
if weekdayOrdinal != other.weekdayOrdinal {
1357-
return false
1358-
}
1359-
if isLeapMonth != other.isLeapMonth {
1360-
return false
1361-
}
1362-
if calendar != other.calendar {
1363-
return false
1364-
}
1365-
if timeZone != other.timeZone {
1366-
return false
1367-
}
1368-
return true
1369-
}
1370-
return false
1316+
guard let other = object as? NSDateComponents else { return false }
1317+
1318+
return self == other
1319+
|| (era == other.era
1320+
&& year == other.year
1321+
&& quarter == other.quarter
1322+
&& month == other.month
1323+
&& day == other.day
1324+
&& hour == other.hour
1325+
&& minute == other.minute
1326+
&& second == other.second
1327+
&& nanosecond == other.nanosecond
1328+
&& weekOfYear == other.weekOfYear
1329+
&& weekOfMonth == other.weekOfMonth
1330+
&& yearForWeekOfYear == other.yearForWeekOfYear
1331+
&& weekday == other.weekday
1332+
&& weekdayOrdinal == other.weekdayOrdinal
1333+
&& isLeapMonth == other.isLeapMonth
1334+
&& calendar == other.calendar
1335+
&& timeZone == other.timeZone)
13711336
}
13721337

13731338
public convenience required init?(coder aDecoder: NSCoder) {

Foundation/NSError.swift

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,8 @@ open class NSError : NSObject, NSCopying, NSSecureCoding, NSCoding {
169169

170170
override open func isEqual(_ object: Any?) -> Bool {
171171
// Pulled from NSObject itself; this works on all platforms.
172-
if let obj = object as? NSError {
173-
return obj === self
174-
}
175-
176-
return false
172+
guard let obj = object as? NSError else { return false }
173+
return obj === self
177174
}
178175
}
179176

Foundation/NSObject.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,8 @@ open class NSObject : NSObjectProtocol, Equatable, Hashable {
186186
/// - Parameter object: The object with which to compare the instance.
187187
/// - Returns: `true` if the instance is equal to `object`, otherwise `false`.
188188
open func isEqual(_ object: Any?) -> Bool {
189-
if let obj = object as? NSObject {
190-
return obj === self
191-
}
192-
return false
189+
guard let obj = object as? NSObject else { return false }
190+
return obj === self
193191
}
194192

195193
/// Returns an integer that can be used as a table address in a hash table structure.

Foundation/NSOrderedSet.swift

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,8 @@ open class NSOrderedSet : NSObject, NSCopying, NSMutableCopying, NSSecureCoding,
3333
}
3434

3535
open override func isEqual(_ object: Any?) -> Bool {
36-
if let orderedSet = object as? NSOrderedSet {
37-
return isEqual(to: orderedSet)
38-
} else {
39-
return false
40-
}
36+
guard let orderedSet = object as? NSOrderedSet else { return false }
37+
return isEqual(to: orderedSet)
4138
}
4239

4340
open func encode(with aCoder: NSCoder) {

Foundation/NSPredicate.swift

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -73,25 +73,23 @@ open class NSPredicate : NSObject, NSSecureCoding, NSCopying {
7373
}
7474

7575
open override func isEqual(_ object: Any?) -> Bool {
76-
if let other = object as? NSPredicate {
77-
if other === self {
78-
return true
79-
} else {
80-
switch (other.kind, self.kind) {
81-
case (.boolean(let otherBool), .boolean(let selfBool)):
82-
return otherBool == selfBool
83-
case (.format, .format):
84-
NSUnimplemented()
85-
case (.metadataQuery, .metadataQuery):
86-
NSUnimplemented()
87-
default:
88-
// NSBlockPredicate returns false even for copy
89-
return false
90-
}
76+
guard let other = object as? NSPredicate else { return false }
77+
78+
if other === self {
79+
return true
80+
} else {
81+
switch (other.kind, self.kind) {
82+
case (.boolean(let otherBool), .boolean(let selfBool)):
83+
return otherBool == selfBool
84+
case (.format, .format):
85+
NSUnimplemented()
86+
case (.metadataQuery, .metadataQuery):
87+
NSUnimplemented()
88+
default:
89+
// NSBlockPredicate returns false even for copy
90+
return false
9191
}
9292
}
93-
94-
return false
9593
}
9694

9795
// Parse predicateFormat and return an appropriate predicate

Foundation/NSTimeZone.swift

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,8 @@ open class NSTimeZone : NSObject, NSCopying, NSSecureCoding, NSCoding {
5454
}
5555

5656
open override func isEqual(_ object: Any?) -> Bool {
57-
if let tz = object as? NSTimeZone {
58-
return isEqual(to: tz._swiftObject)
59-
} else {
60-
return false
61-
}
57+
guard let other = object as? NSTimeZone else { return false }
58+
return isEqual(to: other._swiftObject)
6259
}
6360

6461
open override var description: String {

Foundation/NSURL.swift

Lines changed: 14 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -257,11 +257,8 @@ open class NSURL : NSObject, NSSecureCoding, NSCopying {
257257
}
258258

259259
open override func isEqual(_ object: Any?) -> Bool {
260-
if let url = object as? NSURL {
261-
return CFEqual(_cfObject, url._cfObject)
262-
} else {
263-
return false
264-
}
260+
guard let other = object as? NSURL else { return false }
261+
return CFEqual(_cfObject, other._cfObject)
265262
}
266263

267264
open override var description: String {
@@ -940,13 +937,10 @@ open class NSURLQueryItem : NSObject, NSSecureCoding, NSCopying {
940937
}
941938

942939
open override func isEqual(_ object: Any?) -> Bool {
943-
if let other = object as? NSURLQueryItem {
944-
return other === self
940+
guard let other = object as? NSURLQueryItem else { return false }
941+
return other === self
945942
|| (other.name == self.name
946943
&& other.value == self.value)
947-
}
948-
949-
return false
950944
}
951945

952946
open let name: String
@@ -961,34 +955,16 @@ open class NSURLComponents: NSObject, NSCopying {
961955
}
962956

963957
open override func isEqual(_ object: Any?) -> Bool {
964-
if let other = object as? NSURLComponents {
965-
if scheme != other.scheme {
966-
return false
967-
}
968-
if user != other.user {
969-
return false
970-
}
971-
if password != other.password {
972-
return false
973-
}
974-
if host != other.host {
975-
return false
976-
}
977-
if port != other.port {
978-
return false
979-
}
980-
if path != other.path {
981-
return false
982-
}
983-
if query != other.query {
984-
return false
985-
}
986-
if fragment != other.fragment {
987-
return false
988-
}
989-
return true
990-
}
991-
return false
958+
guard let other = object as? NSURLComponents else { return false }
959+
return self === other
960+
|| (scheme == other.scheme
961+
&& user == other.user
962+
&& password == other.password
963+
&& host == other.host
964+
&& port == other.port
965+
&& path == other.path
966+
&& query == other.query
967+
&& fragment == other.fragment)
992968
}
993969

994970
open func copy(with zone: NSZone? = nil) -> Any {

Foundation/NSURLCredential.swift

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,11 @@ open class URLCredential : NSObject, NSSecureCoding, NSCopying {
106106
}
107107

108108
open override func isEqual(_ object: Any?) -> Bool {
109-
if let other = object as? URLCredential {
110-
return other === self
111-
|| (other._user == self._user
112-
&& other._password == self._password
113-
&& other._persistence == self._persistence)
114-
}
115-
116-
return false
109+
guard let other = object as? URLCredential else { return false }
110+
return other === self
111+
|| (other._user == self._user
112+
&& other._password == self._password
113+
&& other._persistence == self._persistence)
117114
}
118115

119116
/*!

Foundation/NSURLRequest.swift

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -240,17 +240,15 @@ open class NSURLRequest : NSObject, NSSecureCoding, NSCopying, NSMutableCopying
240240
//httBody
241241
//networkServiceType
242242
//httpShouldUsePipelining
243-
if let other = object as? NSURLRequest {
244-
return other === self
245-
|| (other.url == self.url
246-
&& other.mainDocumentURL == self.mainDocumentURL
247-
&& other.httpMethod == self.httpMethod
248-
&& other.cachePolicy == self.cachePolicy
249-
&& other.httpBodyStream == self.httpBodyStream
250-
&& other.allowsCellularAccess == self.allowsCellularAccess
251-
&& other.httpShouldHandleCookies == self.httpShouldHandleCookies)
252-
}
253-
return false
243+
guard let other = object as? NSURLRequest else { return false }
244+
return other === self
245+
|| (other.url == self.url
246+
&& other.mainDocumentURL == self.mainDocumentURL
247+
&& other.httpMethod == self.httpMethod
248+
&& other.cachePolicy == self.cachePolicy
249+
&& other.httpBodyStream == self.httpBodyStream
250+
&& other.allowsCellularAccess == self.allowsCellularAccess
251+
&& other.httpShouldHandleCookies == self.httpShouldHandleCookies)
254252
}
255253

256254
/// Indicates that NSURLRequest implements the NSSecureCoding protocol.

0 commit comments

Comments
 (0)