How to confirm an enumeration to Identifiable protocol in Swift?

Атанас Начков

I'm trying to make a list with the raw values of the cases from an enumeration with the new SwiftUI framework. However, I'm having a trouble with conforming the 'Data' to Identifiable protocol and I really cannot find information how to do it. It tells me "Initializer 'init(_:rowContent:)' requires that 'Data' conform to 'Identifiable'" The stub provides me with an ObjectIdentifier variable in the last extension, but don't know what should I return. Could you tell me how do it? How do I conform Data to Identifiable, so I can make a list with the raw values?

enum Data: String {
    case firstCase = "First string"
    case secondCase = "Second string"
    case thirdCase = "Third string"
}

extension Data: CaseIterable {
    static let randomSet = [Data.firstCase, Data.secondCase]
}

extension Data: Identifiable {
    var id: ObjectIdentifier {
        return //what?
    }

}

//-------------------------ContentView------------------------
import SwiftUI

struct Lala: View {
    var name: String

    var body: some View {
        Text(name)
    }
}

struct ContentView: View {
    var body: some View {
        return List(Data.allCases) { i in
            Lala(name: i.rawValue)
        }
    }
}
Mojtaba Hosseini

When something conforms to Identifiable , it must return something that can be identified by that. So you should return something unique to that case. For String base enum, rawValue is the best option you have:

extension Data: Identifiable {
    var id: String { rawValue }
}

Also enums can usually identified by their selves:

extension Data: Identifiable {
    var id: Data { self }
}

NOTE 1: If you return something that is always unique, like UUID, this means you get a new object each time you get the object and this will kill reusability and can cause epic memory and layout process usage.

Note 2: From Swift 5.1, single line closures don't need return.

Note 3: Try not to use globally known names like Data for your own types. At least use namespace for that like MyCustomNameSpace.Data

Inline mode

You can make any collection iterable inline by one of it's element's keypath:

For example to self:

List (Data.allCases, id:\.self)

or to any other compatible keypath:

List (Data.allCases, id:\.rawValue)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to get the name of enumeration value in Swift?

How to define category bit mask enumeration for SpriteKit in Swift?

Swift: Type does not confirm to protocol 'BooleanType.Protocol'

How to extend a protocol in Swift

How to pass protocol with associated type (generic protocol) as parameter in Swift?

How do you confirm a string only contains numbers in Swift?

How to append Protocol Buffers in Swift?

How to transform array of core data managed objects into an "identifiable" list, in swift? (Xcode 11, Beta 5)

Identifiable protocol in swiftUI... id property... var vs let

Swift protocol inside of protocol

How to create a collection of Identifiable objects similar to Set?

How to implement Identifiable using two enum variables

Why is it necessary to refer to self for the Identifiable protocol?

how to insert a structured identifiable array in a list?

SwiftUI - Custom Identifier with Identifiable Protocol

Initializer `init(:_rowContent:)` requires that `Type` confirm to `Identifiable`

does not confirm to protocol UITableViewDataSource in Swift , Why?

How to use a protocol to SKPSMTPMessageDelegate in Swift?

how to increment a swift int enumeration

How to conform a class to a protocol in Swift and can protocol methods be overridden?

How to send a confirm email from swift

How to check scanf input that is not identifiable by %

Swift: How to pass the Generic protocol as a parameter for another protocol

swift generic sequence with identifiable constraint on elements

How to append an identifiable struct list?

Protocol oriented Programming Swift - Identifiable

Swift Conform to Identifiable with existing property

Swift enum conformance to identifiable: Type doesn't conform to Identifiable protocol

Using SwiftUI ForEach to iterate over [any Protocol] where said protocol is Identifiable