Xcode 9 and Xcode 10 giving different results, even with same swift version

前端 未结 3 1914
青春惊慌失措
青春惊慌失措 2020-12-06 17:42

I am running this code in both xcode 9.3 and xcode 10 beta 3 playground

import Foundation

public protocol EnumCollection: Hashable {
    static func cases()         


        
3条回答
  •  抹茶落季
    2020-12-06 18:13

    If you use hashValue of an enum to determine case values (position or id), it's a wrong approach since it doesn't guarantee to return sequential int values, 0,1,2... It is not working anymore from swift 4.2

    For example, If you use an enum like this :

    enum AlertResultType {
        case ok, cancel 
    }
    

    hashValue of this enum might return a large int value instead of 0 (ok) and 1 (cancel).

    So you may need to declare the enum type more precisely and use rowValue. For example

    enum AlertResultType : Int {
        case ok = 0, cancel = 1
    }
    

提交回复
热议问题