This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) { | |
guard | |
challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust, | |
let serverTrust = challenge.protectionSpace.serverTrust | |
else { | |
completionHandler(URLSession.AuthChallengeDisposition.cancelAuthenticationChallenge, nil) | |
return | |
} | |
guard |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Key: Hashable { | |
func hash(into hasher: inout Hasher) { | |
hasher.combine(self.id) | |
} | |
func hash(into hasher: inout Hasher) { | |
hasher.combine(self.id) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let key1 = Key(id: 1, title: "Key1") | |
let key2 = Key(id: 1, title: "Key2") | |
dict[key1] = 1058 | |
dict[key2] = 368 | |
print(dict.count) // 2 | |
print(key1 == key2) // false | |
print(key1.hashValue == key2.hashValue) // true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Key { | |
let id: Int | |
let title: String | |
} | |
extension Key: Hashable { | |
func hash(into hasher: inout Hasher) { | |
hasher.combine(self.id) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Using hasher. Actually, you will never use it directly. | |
var hasher = Hasher() | |
hasher.combine("Hello") | |
hasher.combine("World") | |
hasher.combine(2020) | |
let hashValue = hasher.finalize() | |
// This is the correct way to implement Hashable. | |
extension YourStruct: Hashable { | |
func hash(into hasher: inout Hasher) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// A point in an x-y coordinate system. | |
struct GridPoint { | |
var x: Int | |
var y: Int | |
} | |
extension GridPoint: Hashable { | |
static func == (lhs: GridPoint, rhs: GridPoint) -> Bool { | |
return lhs.x == rhs.x && lhs.y == rhs.y | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let pushups = Array<Pushup>() | |
// This doesn't raise any error because built-in | |
// generic types are covariant. | |
var exercises: Array<Exercise> = pushups |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Activity<T> {} | |
var pushup = Activity<Pushup>() | |
// You can't do this, it raises the error: | |
// Cannot assign value of type 'Activity<Pushup>' to type 'Activity<Exercise>' | |
var activity: Activity<Exercise> = pushup |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Exercise { | |
func perform() {} | |
func stop() {} | |
} | |
class Pushup: Exercise { | |
override func perform() {} | |
override func stop() {} | |
} |
NewerOlder