如何在少数情况下包含额外属性的情况下创建枚举

时间:2019-06-20 08:01:19

标签: ios swift

我需要上传4个文件对象。带有日期的文件很少,带有图像的文件很少。我应该使用什么数据结构?

我试图创建一个枚举

{
  "name": "ABCDEFGHIJKLMNOPQRSTUVWXYZA"
}

{
  "name": "ERTERER"
}

我试图创建一个结构

enum File: String, CaseIterable {
    case a = "A"
    case b = "B"
    case c = "C"
    case d = "D"
    var size: String//enum cannot contain stored properties
}

4 个答案:

答案 0 :(得分:1)

错误说明了一切

  

枚举不能包含存储的属性

理想情况下,您是为枚举属性提供值的人。意味着它们应该是read-only。但是,您可以创建枚举的新实例。

这是一个例子:

enum File: String, CaseIterable {
    case a = "A"
    case b = "B"
    case c = "C"
    case d = "D"

    var size: String {
        switch self {
        case .a: return "Some size for A"
        case .b: return "Some size for B"
        case .c: return "Some size for C"
        case .d: return "Some size for D"
        }
    }
}

let fileA = File(rawValue: "A")
let fileB = File(rawValue: "B")

let size = fileA.size //Some size for A
let size = fileB.size //Some size for B

答案 1 :(得分:1)

如果回答是“如何创建具有额外属性的枚举”,则可以编写如下内容:

<from uri="timer://foo?repeatCount=1"/>

此外,请注意,带有原始类型(在您的情况下为String)的枚举不能包含额外的参数。

PS:说实话,在您遇到的情况下使用枚举没有任何优势

答案 2 :(得分:1)

您应在此处同时使用enumstruct

// Enum with associated values for differences
enum FileType {
    case withDate(date: Date)
    case withImage(image: UIImage)
}

// Struct to bind common
struct File {
    var name: String
    var fileType: FileType
}

然后您可以定义文件:

extension File {
    // Files with date
    static let a = File(name: "a", fileType: .withDate(date: Date()))
    static let b = File(name: "b", fileType: .withDate(date: Date()))

    // Files with image
    static let c = File(name: "c", fileType: .withImage(image: UIImage()))
    static let d = File(name: "d", fileType: .withImage(image: UIImage()))
}

对于访问dateimage的简单方法,您可以声明这些方便的扩展名:

extension FileType {
    var date: Date? {
        if case let .withDate(date) = self {
            return date
        } else {
            return nil
        }
    }

    var image: UIImage? {
        if case let .withImage(image) = self {
            return image
        } else {
            return nil
        }
    }
}

extension File {
    var date: Date? {
        return fileType.date
    }

    var image: UIImage? {
        return fileType.image
    }
}

然后像这样使用它:

let date = File.a.date

答案 3 :(得分:1)

这可以帮助您-

    enum File: String, CaseIterable {
    case a = "A"
    case b = "B"
    case c = "C"
    case d = "D"

    static let sizeMapper: [File: String] = [
        .a: "Size A",
        .b: "Size B"
        ]
    static let nameMapper: [File: String] = [
        .a: "Name A",
        .b: "Name B",
        .c: "Name C",
        .d: "Name D"
        ]
    static let imageMapper: [File: UIImage] = [
        .c: UIImage(),
        .d: UIImage()
        ]

    var size: String {
        return File.sizeMapper[self] ?? ""
    }
    var name: String {
        return File.nameMapper[self] ?? ""
    }
    var image: UIImage {
        return File.imageMapper[self] ?? UIImage()
    }
}

这样称呼它-

        let size = File.a.size
        print(size)
        let name = File.a.name
        print(name)
        let image = File.c.image
        print(image)