hashable

Make struct Hashable?

被刻印的时光 ゝ 提交于 2019-11-28 21:08:25
I'm trying to create a dictionary of the sort [petInfo : UIImage]() but I'm getting the error Type 'petInfo' does not conform to protocol 'Hashable' . My petInfo struct is this: struct petInfo { var petName: String var dbName: String } So I want to somehow make it hashable but none of its components are an integer which is what the var hashValue: Int requires. How can I make it conform to the protocol if none of its fields are integers? Can I use the dbName if I know it's going to be unique for all occurrences of this struct? Simply return dbName.hashValue from your hashValue function. FYI -

How does Dictionary use the Equatable protocol in Swift?

☆樱花仙子☆ 提交于 2019-11-27 21:40:39
问题 In order to solve this question, I have been playing around with a custom struct that implements the Hashable Protocol. I'm trying to see how many times the equivalency operator overload ( == ) gets called depending on if there is a hash collision or not when populating a Dictionary . Update @matt wrote a much cleaner example of a custom struct that implements the Hashable protocol and shows how often hashValue and == get called. I am copying his code below. To see my original example, check

Make struct Hashable?

孤者浪人 提交于 2019-11-27 13:29:55
问题 I'm trying to create a dictionary of the sort [petInfo : UIImage]() but I'm getting the error Type 'petInfo' does not conform to protocol 'Hashable' . My petInfo struct is this: struct petInfo { var petName: String var dbName: String } So I want to somehow make it hashable but none of its components are an integer which is what the var hashValue: Int requires. How can I make it conform to the protocol if none of its fields are integers? Can I use the dbName if I know it's going to be unique

Swift: 'Hashable.hashValue' is deprecated as a protocol requirement;

老子叫甜甜 提交于 2019-11-26 21:09:04
问题 I've been facing following issue (it's just a warning) with my iOS project. 'Hashable.hashValue' is deprecated as a protocol requirement; conform type 'ActiveType' to 'Hashable' by implementing 'hash(into:)' instead Xcode 10.2 Swift 5 Source Code: public enum ActiveType { case mention case hashtag case url case custom(pattern: String) var pattern: String { switch self { case .mention: return RegexParser.mentionPattern case .hashtag: return RegexParser.hashtagPattern case .url: return

Why can't I use a list as a dict key in python?

霸气de小男生 提交于 2019-11-26 01:43:40
问题 I\'m a bit confused about what can/can\'t be used as a key for a python dict. dicked = {} dicked[None] = \'foo\' # None ok dicked[(1,3)] = \'baz\' # tuple ok import sys dicked[sys] = \'bar\' # wow, even a module is ok ! dicked[(1,[3])] = \'qux\' # oops, not allowed So a tuple is an immutable type but if I hide a list inside of it, then it can\'t be a key.. couldn\'t I just as easily hide a list inside a module? I had some vague idea that that the key has to be \"hashable\" but I\'m just going