HomeiOS Developmentios - Create a Generic View that passes an enum as a...

ios – Create a Generic View that passes an enum as a parameter


In Xcode 15, the primary requirement is so as to add T.AllCases.Index: BinaryInteger.

firstIndex returns an Elective, so it’s best to cope with that.

Lastly, this line would not make sense:

let currentIndex = someEnum.allCases.firstIndex(of: someEnum as! T)

someEnum won’t ever have an index of someEnum — the requirement so as to add as! T must be a tipoff that one thing is fallacious right here. Nonetheless, it is not clear what your intent is or the best way to repair it.

enum MyEnumType: String, CaseIterable, Identifiable {
    case one, two, three
    
    var id: String {
        rawValue
    }
}

struct TestView: View {
    var physique: some View {
        GenericView(someEnum: MyEnumType.self) {
            Textual content("Take a look at")
        }
    }
}

struct GenericView<Content material: View, T: CaseIterable & Identifiable & Equatable>: View the place T.AllCases: RandomAccessCollection, T.AllCases.Index: BinaryInteger {
    let someEnum: T.Kind
    let content material: () -> Content material
    var physique: some View {
        ForEach(someEnum.allCases) { circumstances  in
            GeometryReader { reader in
                let screenIndex = someEnum.allCases.firstIndex(of: circumstances) ?? 0
                let currentIndex = 1 // someEnum.allCases.firstIndex(of: someEnum as! T)!
                let offset = CGFloat(Int(screenIndex) - currentIndex) * reader.dimension.width
                
                content material()
                    .offset(x:offset)
            }
        }
    }
}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments