What is the difference between { } and = when assigning an enum to a variable in Swift?

Viewed 32

What is the difference between { } and = when assigning an enum to a variable in Swift?

Why would you say var type: ItemType { .recipe } over var type: ItemType = .video.

Does the {} indicate that it is a computed property?

Also, is the { get } needed after the type in the protocol?

enum ItemType: String, Decodable {
case video
case recipe
}

protocol Item: Decodable {
    var type: ItemType { get }
    var title: String { get }
    var imageURL: URL { get }
}

struct Video: Item {
    var type: ItemType = .video
    var title: String
    var imageURL: URL
    var url: URL
    var duration: String
    var resolution: String
}

struct Recipe: Item {
    var type: ItemType { .recipe }
    var title: String
    var imageURL: URL
    var text: String
    var ingredients: [String]
}
1 Answers

Does the {} indicate that it is a computed property?

Yes.

Also, is the { get } needed after the type in the protocol?

To specify that the property, whether it be computed or stored, needs to support at least a getter. The setter is optional.

Related