Quinn
Quinn

Reputation: 9404

SwiftUI List not recognizing object type

So I'm trying to make a List in SwiftUI like this:

struct DetailsView: View {
    var piis = [IDPiece]()

    var body: some View {
        List(piis, id: \.identifier) { pii in
            Text( pii.label )
        }
    }
}

where IDPiece looks like:

struct IDPiece: Equatable {
    init() {}

    init(claim: Claim) {
        self.document = claim.document
        self.identifier = claim.identifier
        self.claimUID = claim.claimUID
        self.label = claim.label
    }

    var document: DocumentType = .na
    var identifier: String = ""
    var claimUID: String = ""
    var label: String?
}

But I keep receiving the following error on the line where I initialize the list:

Type '_' has no member 'identifier'

It doesn't seem to be parsing the type of object contains in my piis list. Anyone know why that may be?

Upvotes: 0

Views: 178

Answers (1)

Rob Napier
Rob Napier

Reputation: 299275

SwiftUI compiler errors are generally useless (this will improve over time, but today they're useless). Your problem has nothing to do with \.identifier. The problem is you have an optional .label, but you don't handle the case where it's nil. Almost certainly you should just make label non-optional. But if it needs to be optional (if you treat nil differently than empty in some place), then you need to do something about that, such as:

Text(pii.label ?? "N/A")

Upvotes: 4

Related Questions