Swift中的Enum vs Struct

xoshrz7s  于 2023-06-21  发布在  Swift
关注(0)|答案(1)|浏览(110)

下面我用HTTPStatus实现了Enum和Struct来演示这个例子。我想知道关于查找特定状态和阅读属性,哪一个在读取和内存效率方面更有性能?
我注意到的是排除了代码风格,长度,

  1. Enum也像Set of struct那样通过id搜索特定状态。
  2. Enum使用Switch case来读取特定case的属性,即使我们已经站在HTTPStatus上,而struct没有。

枚举:

enum HTTPStatus: UInt16 {
    
    case ok = 200
    case created = 201
    case accepted = 202
    
    var label: String {
        
        switch self {
        case .ok:
            return "Ok"
        case .created:
            return "Created"
        case .accepted:
            return "Accepted"
        }
    }
    
    var description: String {
        
        switch self {
        case .ok:
            return "\(self.rawValue) - \(self.label) is a standard response for successful HTTP requests. The actual response will depend on the request method used. In a GET request, the response will contain an entity corresponding to the requested resource. In a POST request, the response will contain an entity describing or containing the result of the action."
        case .created:
            return "\(self.rawValue) - \(self.label) means the request has been fulfilled, resulting in the creation of a new resource."
        case .accepted:
            return "\(self.rawValue) - \(self.label) means the request has been accepted for processing, but the processing has not been completed. The request might or might not be eventually acted upon, and may be disallowed when processing occurs."
        }
    }
    
    var error: ErrorType {
        
        switch self.rawValue {
            
        case 200...299: return .none
            
        default: return .server
        }
    }
    
    enum ErrorType: UInt8 {
        
        case server
        case client
        case none
    }
}

let statusCode: UInt16 = 500

if let status = HTTPStatus(rawValue: statusCode) {
    
    print(status.description)
}

结构:

struct HTTPStatus: Equatable, Hashable {
    
    let code: UInt16
    let shortLabel: String
    
    private let longLabel: String
    
    init(_ code: UInt16, shortLabel: String, longLabel: String) {
        
        self.code = code
        self.shortLabel = shortLabel
        self.longLabel = longLabel
    }
    
    var description: String {
        
        return "\(self.code) - \(self.shortLabel) \(self.longLabel)"
    }
    
    func hash(into hasher: inout Hasher) {
        
        hasher.combine(self.code)
    }
    
    var isSuccess: Bool { return self.code < 300}
    
    var isClientError: Bool { return self.code > 399}

    var isServerError: Bool { return self.code > 499}
}

let httpStatuses: Set<HTTPStatus> = [

    .init(200, shortLabel: "Ok", longLabel: "is a standard response for successful HTTP requests. The actual response will depend on the request method used. In a GET request, the response will contain an entity corresponding to the requested resource. In a POST request, the response will contain an entity describing or containing the result of the action.")
]

let statusCode: UInt16 = 500

if let status = httpStatuses.first(where: {$0.code == statusCode}) {
    
    print(status.description)
}

假设我们必须在枚举和结构中实现所有HTTPStatusCode。在这种情况下,哪一个表现更好,
1.每个状态的内存分配
1.按状态代码搜索值或事例
1.在枚举和结构上阅读属性和为该属性值分配内存的时间?
总的来说,我只是想知道我是否需要枚举。

cygmwpex

cygmwpex1#

Swift枚举是值类型,就像结构一样。除非放在某种堆分配的缓冲区(如数组)中,否则它们是堆栈分配的。无论是枚举还是结构,都不会从自动内存管理中获得开销。在性能方面,它们应该具有可比性。
总的来说,我只是想知道我是否需要枚举。
你的对象是否表示一组离散的状态?使用枚举。它是给定类型的任意值的容器吗?使用结构。

相关问题