0

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?

1 Answer 1

4

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")
Sign up to request clarification or add additional context in comments.

1 Comment

Lol, I can't believe that actually fixed it... I thought i had tried hard coding the value in the text and it hadn't worked but I guess I had some other error when i tried that. Anyway thanks, that did solve the problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.