3

I have a problem and don't know how to solve it. I have several enum's that return a string, used every where in the project, on a separate textPhrases file.

I want to use some of them to fill a picker ( segmented ).

I trying to understand how should I use and setup the tag element.

I was thinking to have a static var that return a tuple with [String:Int] [rawCaseValue, index], but get the folowing error - Type '(String, Int)' cannot conform to 'Hashable'; only struct/enum/class types can conform to protocols

Here is a sample of my code. The second picker works perfect, but I want to refactor that to use the enum provided Strings.

//  TextPhrases.swift
//  Zinnig

import SwiftUI

enum SalutationClient: String, CaseIterable {
    case noChoice = "…"
    case sir = "Meneer"
    case mrs = "Mevrouw"
    case miss = "Mejuffrouw"

    static let all = SalutationClient.allCases.map { $0.rawValue }

    static var allIndexed : [(String, Int)]{
    var tupleArray: [(String, Int)] = []
    var ind : Int = 0

       for salut in SalutationClient.all //allCases.map({ $0.rawValue })
        {
           tupleArray.append( (salut, ind))
           ind += 1
        }
        return tupleArray
   }
}


//  PersonContentView.swift
//  Zinnig


/// Main Person view
struct PersonContentView: View {

@State private var selectedSalutation = 0

/// next line will be removed and replace by an enum with Strings
@State private var dayPart: [String]  = ["…", "ochtend", "middag", "avond", "nacht"]
@State private var selectedDayPart = 0

...
Form {
        Picker(selection: $selectedSalutation, label: Text("Aanspreken met;")) {
            ForEach ( SalutationClient.allIndexed, id: \.self ) { salutTuple in
                    Text((salutTuple.0)).tag((salutTuple.1))
                }
        }.pickerStyle(SegmentedPickerStyle())

        Picker(selection: $selectedDayPart, label: Text("Selecteer een dagdeel")) {
            ForEach(0 ..< dayPart.count) {
                 Text(self.dayPart[$0]).tag($0)
            }
        }.pickerStyle(SegmentedPickerStyle())
    }

The error is - Type '(String, Int)' cannot conform to 'Hashable'; only struct/enum/class types can conform to protocols

How should I proceed to get this working?

Thank you

2 Answers 2

2

The following code did solve my problem.

//  TextPhrases.swift
//  Zinnig

enum SalutationClient: String, CaseIterable, Hashable {
    case noChoiceMade = "…"
    case sir = "Meneer"
    case mrs = "Mevrouw"
    case miss = "Mejuffrouw"

    static let all = SalutationClient.allCases.map { $0.rawValue }

    struct TupleStruct: Hashable {
        var salutation: String
        var index: Int
    }

    static var allIndexed : [TupleStruct]{
        var tupleArray : [TupleStruct] = []
        var ind : Int = 0

        for salut in SalutationClient.all       //allCases.map({ $0.rawValue })
        {
            tupleArray = tupleArray + [TupleStruct(salutation: salut, index: ind )]
            ind += 1
        }
        print(tupleArray)
        return tupleArray
    }
}

//  PersonContentView.swift
//  Zinnig

Picker(selection: $selectedSalutation, label: Text("Select een persoon type;")) {
    ForEach(SalutationClient.allIndexed, id: \.self) { salut in
        Text(salut.salutation).tag(salut.index)
    }
}.pickerStyle(SegmentedPickerStyle())

If someone has a comment on this, please let me know. I am always open for better and new things.

Sign up to request clarification or add additional context in comments.

Comments

2

You've made it much more complicated than you need to. Take a look at Apple's documentation which speaks to your use case directly.

https://developer.apple.com/documentation/swiftui/picker

1 Comment

You are correct - this is terser than the OP's own solution, however, I think you should quote the answer as well as link to it, since links break in time. Then the OP could mark it as the correct answer. Also it's shorter because it uses several implicit Swift techs: Identifiable, CaseIterable and the fact that an enum String's rawValue is the case - you could explain these, which the Apple site does not, and be really useful!

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.