• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

[Swift]实现Swift的两个协议:Hashable协议和Equatable协议(Typedoesnotconformtopro ...

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/ 
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10815581.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

【Command 】+【鼠标右键】-> 【Jump to Definition】

Hashable协议:

 1 public protocol Hashable : Equatable {
 2 
 3     /// The hash value.
 4     ///
 5     /// Hash values are not guaranteed to be equal across different executions of
 6     /// your program. Do not save hash values to use during a future execution.
 7     ///
 8     /// - Important: `hashValue` is deprecated as a `Hashable` requirement. To
 9     ///   conform to `Hashable`, implement the `hash(into:)` requirement instead.
10     var hashValue: Int { get }
11 
12     /// Hashes the essential components of this value by feeding them into the
13     /// given hasher.
14     ///
15     /// Implement this method to conform to the `Hashable` protocol. The
16     /// components used for hashing must be the same as the components compared
17     /// in your type's `==` operator implementation. Call `hasher.combine(_:)`
18     /// with each of these components.
19     ///
20     /// - Important: Never call `finalize()` on `hasher`. Doing so may become a
21     ///   compile-time error in the future.
22     ///
23     /// - Parameter hasher: The hasher to use when combining the components
24     ///   of this instance.
25     func hash(into hasher: inout Hasher)
26 }

翻译版本:

 1 public protocol Hashable : Equatable {
 2     ///散列值。
 3     //
 4     ///不能保证哈希值在不同的
 5     ///你的程序。不要保存哈希值以在将来执行时使用。
 6     //
 7     ///-重要提示:“hashvalue”被弃用为“hashable”要求。到
 8     ///符合“hashable”,改为实现“hash(into:)”要求。
 9 
10     var hashValue: Int { get }
11 
12     ///通过将此值的基本组件放入
13     ///给定哈希。
14     //
15     ///实现此方法以符合“hashable”协议。这个
16     ///用于散列的组件必须与比较的组件相同
17     ///在类型的`=`运算符实现中。调用'hasher.combine(:)`
18     ///每个组件。
19     //
20     ///-重要提示:不要在“hasher”上调用“finalize()”。这样做可能会成为
21     ///将来出现编译时错误。
22     //
23     ///-参数散列器:组合组件时要使用的散列器
24     ///此实例的。
25 
26     func hash(into hasher: inout Hasher)
27 }

Equatable 协议:

  1 /// A type-erased hashable value.
  2 
  3 ///
  4 
  5 /// The `AnyHashable` type forwards equality comparisons and hashing operations
  6 
  7 /// to an underlying hashable value, hiding its specific underlying type.
  8 
  9 ///
 10 
 11 /// You can store mixed-type keys in dictionaries and other collections that
 12 
 13 /// require `Hashable` conformance by wrapping mixed-type keys in
 14 
 15 /// `AnyHashable` instances:
 16 
 17 ///
 18 
 19 ///     let descriptions: [AnyHashable: Any] = [
 20 
 21 ///         AnyHashable("?"): "emoji",
 22 
 23 ///         AnyHashable(42): "an Int",
 24 
 25 ///         AnyHashable(Int8(43)): "an Int8",
 26 
 27 ///         AnyHashable(Set(["a", "b"])): "a set of strings"
 28 
 29 ///     ]
 30 
 31 ///     print(descriptions[AnyHashable(42)]!)      // prints "an Int"
 32 
 33 ///     print(descriptions[AnyHashable(43)])       // prints "nil"
 34 
 35 ///     print(descriptions[AnyHashable(Int8(43))]!) // prints "an Int8"
 36 
 37 ///     print(descriptions[AnyHashable(Set(["a", "b"]))]!) // prints "a set of strings"
 38 
 39 public struct AnyHashable {
 40 
 41 
 42 
 43/// Creates a type-erased hashable value that wraps the given instance.
 44 
 45///
 46 
 47/// The following example creates two type-erased hashable values: `x` wraps
 48 
 49/// an `Int` with the value 42, while `y` wraps a `UInt8` with the same
 50 
 51/// numeric value. Because the underlying types of `x` and `y` are
 52 
 53/// different, the two variables do not compare as equal despite having
 54 
 55/// equal underlying values.
 56 
 57///
 58 
 59///     let x = AnyHashable(Int(42))
 60 
 61///     let y = AnyHashable(UInt8(42))
 62 
 63///
 64 
 65///     print(x == y)
 66 
 67///     // Prints "false" because `Int` and `UInt8` are different types
 68 
 69///
 70 
 71///     print(x == AnyHashable(Int(42)))
 72 
 73///     // Prints "true"
 74 
 75///
 76 
 77/// - Parameter base: A hashable value to wrap.
 78 
 79public init<H>(_ base: H) where H : Hashable
 80 
 81 
 82 
 83/// The value wrapped by this instance.
 84 
 85///
 86 
 87/// The `base` property can be cast back to its original type using one of
 88 
 89/// the casting operators (`as?`, `as!`, or `as`).
 90 
 91///
 92 
 93///     let anyMessage = AnyHashable("Hello world!")
 94 
 95///     if let unwrappedMessage = anyMessage.base as? String {
 96 
 97///         print(unwrappedMessage)
 98 
 99///     }
100 
101///     // Prints "Hello world!"
102 
103public var base: Any { get }
104 
105 
106 
107public static func != (lhs: AnyHashable, rhs: AnyHashable) -> Bool
108 
109 }
110 
111 
112 
113 extension AnyHashable : Equatable {
114 
115 
116 
117/// Returns a Boolean value indicating whether two type-erased hashable
118 
119/// instances wrap the same type and value.
120 
121///
122 
123/// Two instances of `AnyHashable` compare as equal if and only if the
124 
125/// underlying types have the same conformance to the `Equatable` protocol
126 
127/// and the underlying values compare as equal.
128 
129///
130 
131/// The following example creates two type-erased hashable values: `x` wraps
132 
133/// an `Int` with the value 42, while `y` wraps a `UInt8` with the same
134 
135/// numeric value. Because the underlying types of `x` and `y` are
136 
137/// different, the two variables do not compare as equal despite having
138 
139/// equal underlying values.
140 
141///
142 
143///     let x = AnyHashable(Int(42))
144 
145///     let y = AnyHashable(UInt8(42))
146 
147///
148 
149///     print(x == y)
150 
151///     // Prints "false" because `Int` and `UInt8` are different types
152 
153///
154 
155///     print(x == AnyHashable(Int(42)))
156 
157///     // Prints "true"
158 
159///
160 
161/// - Parameters:
162 
163///   - lhs: A type-erased hashable value.
164 
165///   - rhs: Another type-erased hashable value.
166 
167public static func == (lhs: AnyHashable, rhs: AnyHashable) -> Bool
168 
169 }
170 
171 
172 
173 extension AnyHashable : Hashable {
174 
175 
176 
177/// The hash value.
178 
179public var hashValue: Int { get }
180 
181 
182 
183/// Hashes the essential components of this value by feeding them into the
184 
185/// given hasher.
186 
187///
188 
189/// - Parameter hasher: The hasher to use when combining the components
190 
191///   of this instance.
192 
193public func hash(into hasher: inout Hasher)
194 
195 }
196 
197 
198 
199 extension AnyHashable : CustomStringConvertible {
200 
201 
202 
203/// A textual representation of this instance.
204 
205///
206 
207/// Calling this property directly is discouraged. Instead, convert an
208 
209/// instance of any type to a string by using the `String(describing:)`
210 
211/// initializer. This initializer works with any type, and uses the custom
212 
213/// `description` property for types that conform to
214 
215/// `CustomStringConvertible`:
216 
217///
218 
219///     struct Point: CustomStringConvertible {
220 
221///         let x: Int, y: Int
222 
223///
224 
225///         var description: String {
226 
227///             return "(\(x), \(y))"
228 
229///         }
230 
231///     }
232 
233///
234 
235///     let p = Point(x: 21, y: 30)
236 
237///     let s = String(describing: p)
238 
239///     print(s)
240 
241///     // Prints "(21, 30)"
242 
243///
244 
245/// The conversion of `p` to a string in the assignment to `s` uses the
246 
247/// `Point` type's `description` property.
248 
249public var description: String { get }
250 
251 }
252 
253 
254 
255 extension AnyHashable : CustomDebugStringConvertible {
256 
257 
258 
259/// A textual representation of this instance, suitable for debugging.
260 
261///
262 
263/// Calling this property directly is discouraged. Instead, convert an
264 
265/// instance of any type to a string by using the `String(reflecting:)`
266 
267/// initializer. This initializer works with any type, and uses the custom
268 
269/// `debugDescription` property for types that conform to
270 
271/// `CustomDebugStringConvertible`:
272 
273///
274 
275///     struct Point: CustomDebugStringConvertible {
276 
277///         let x: Int, y: Int
278 
279///
280 
281///         var debugDescription: String {
282 
283///             return "(\(x), \(y))"
284 
285///         }
286 
287///     }
288 
289///
290 
291///     let p = Point(x: 21, y: 30)
292 
293///     let s = String(reflecting: p)
294 
295///     print(s)
296 
297///     // Prints "(21, 30)"
298 
299///
300 
301/// The conversion of `p` to a string in the assignment to `s` uses the
302 
303/// `Point` type's `debugDescription` property.
304 
305public var debugDescription: String { get }
306 
307 }
308 
309 
310 
311 extension AnyHashable : CustomReflectable {
312 
313 
314 
315/// The custom mirror for this instance.
316 
317///
318 
319/// If this type has value semantics, the mirror should be unaffected by
320 
321/// subsequent mutations of the instance.
322 
323public var customMirror: Mirror { get }
324 
325 }
326 
327 
328 
329 /// A type that can be hashed into a `Hasher` to produce an integer hash value.
330 
331 ///
332 
333 /// You can use any type that conforms to the `Hashable` protocol in a set or as
334 
335 /// a dictionary key. Many types in the standard library conform to `Hashable`:
336 
337 /// Strings, integers, floating-point and Boolean values, and even sets are
338 
339 /// hashable by default. Some other types, such as optionals, arrays and ranges
340 
341 /// automatically become hashable when their type arguments implement the same.
342 
343 ///
344 
345 /// Your own custom types can be hashable as well. When you define an
346 
347 /// enumeration without associated values, it gains `Hashable` conformance
348 
349 /// automatically, and you can add `Hashable` conformance to your other custom
350 
351 /// types by implementing the `hash(into:)` method. For structs whose stored
352 
353 /// properties are all `Hashable`, and for enum types that have all-`Hashable`
354 
355 /// associated values, the compiler is able to provide an implementation of
356 
357 /// `hash(into:)` automatically.
358 
359 ///
360 
361 /// Hashing a value means feeding its essential components into a hash function,
362 
363 /// represented by the `Hasher` type. Essential components are those that
364 
365 /// contribute to the type's implementation of `Equatable`. Two instances that
366 
367 /// are equal must feed the same values to `Hasher` in `hash(into:)`, in the
368 
369 /// same order.
370 
371 ///
372 
373 /// Conforming to the Hashable Protocol
374 
375 /// ===================================
376 
377 ///
378 
379 /// To use your own custom type in a set or as the key type of a dictionary,
380 
381 /// add `Hashable` conformance to your type. The `Hashable` protocol inherits
382 
383 /// from the `Equatable` protocol, so you must also satisfy that protocol's
384 
385 /// requirements.
386 
387 ///
388 
389 /// The compiler automatically synthesizes your custom type's `Hashable` and
390 
391 /// requirements when you declare `Hashable` conformance in the type's original
392 
393 /// declaration and your type meets these criteria:
394 
395 ///
396 
397 /// - For a `struct`, all its stored properties must conform to `Hashable`.
398 
399 /// - For an `enum`, all its associated values must conform to `Hashable`. (An
400 
401 ///   `enum` without associated values has `Hashable` conformance even without
402 
403 ///   the declaration.)
404 
405 ///
406 
407 /// To customize your type's `Hashable` conformance, to adopt `Hashable` in a
408 
409 /// type that doesn't meet the criteria listed above, or to extend an existing
410 
411 /// type to conform to `Hashable`, implement the `hash(into:)` method in your
412 
413 /// custom type.
414 
415 ///
416 
417 /// In your `hash(into:)` implementation, call `combine(_:)` on the provided
418 
419 /// `Hasher` instance with the essential components of your type. To ensure
420 
421 /// that your type meets the semantic requirements of the `Hashable` and
422 
423 /// `Equatable` protocols, it's a good idea to also customize your type's
424 
425 /// `Equatable` conformance to match.
426 
427 ///
428 
429 /// As an example, consider a `GridPoint` type that describes a location in a
430 
431 /// grid of buttons. Here's the initial declaration of the `GridPoint` type:
432 
433 ///
434 
435 ///     /// A point in an x-y coordinate system.
436 
437 ///     struct GridPoint {
438 
439 ///         var x: Int
440 
441 ///         var y: Int
442 
443 ///     }
444 
445 ///
446 
447 /// You'd like to create a set of the grid points where a user has already
448 
449 /// tapped. Because the `GridPoint` type is not hashable yet, it can't be used
450 
451 /// in a set. To add `Hashable` conformance, provide an `==` operator function
452 
453 /// and implement the `hash(into:)` method.
454 
455 ///
456 
457 ///     extension GridPoint: Hashable {
458 
459 ///         static func == (lhs: GridPoint, rhs: GridPoint) -> Bool {
460 
461 ///             return lhs.x == rhs.x && lhs.y == rhs.y
462 
463 ///         }
464 
465 ///
466 
467 /// 
                       
                    
                    

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Swift与Objective-C的兼容“黑魔法”:@objc和Dynamic发布时间:2022-07-13
下一篇:
Swift-3.0去掉C风格循环发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap