我知道这是一个奇怪的标题,但有很多帖子的标题相似,但完全不同的问题.大多数人在他们的视图中编写其他东西,而不是View个代码,这是我没有做的(据我所知).

我正在try 让Picker与其他BinaryInteger类型兼容,因为除了Int之外,它不能与其他任何类型兼容,而且我在使用预览时遇到了一些问题.代码如下:

import SwiftUI

struct CompatibilityPicker<Label, SelectionValue, Content> : View where Label : StringProtocol, SelectionValue : BinaryInteger, Content : View {
    
    var content : () -> Content
    var label : Label
    
    @Binding private var _selection : SelectionValue
    
    private var selection: Binding<Int> { Binding<Int>(
        get: {
            Int(_selection)
        },
        set: {
            self._selection = SelectionValue($0)
        })
    }
    
    init(_ label : Label, selection : SelectionValue, content : @escaping () -> Content) {
        self.label = label
        self._selection = selection
        self.content = content
    }
    
    var body: some View {
        Picker(label, selection: selection, content: content)
    }
}

struct CompatibilityPicker_Previews: PreviewProvider {
    @State static var selection : UInt8 = 4
    
    static var previews: some View {
        CompatibilityPicker("Difficulty", selection: selection) { //error : Type'()' cannot conform to 'View'
            Text("Easy").tag(0)
            Text("Normal").tag(1)
            Text("Hard").tag(2)
        }
    }
}

怎么回事?我有一个正常的Picker,它使用完全相同的语法,并且工作正常,我不知道我做错了什么.


多亏了@RobMayoff的解决方案,我又领先了一步,尽管出现了一些看起来毫无意义的错误,但通过cmd+Shift+k:

init(_ label : Label, selection : SelectionValue, @ViewBuilder content : @escaping () -> Content) {
    self.content = content
    self.label = label
    self._selection = selection //variable self._selection used before initialised
    // This stays on this line if I change the order,
} // Return from initializer without initialising all stored properties
// That is not true, as far as I can tell

推荐答案

以下是基于我之前的答案的一个有效变体.使用Xcode 14/iOS 16进行测试

有问题的代码中有几个错误: Select 值没有绑定,内容没有视图构建器,隐藏绑定自动生成的属性冲突,等等,所以我只更新了一点我以前的解决方案.

demo

struct CompatibilityPicker<Label, SelectionValue, Content>: View where Label : StringProtocol, SelectionValue : BinaryInteger, Content : View {

    var label : Label
    @Binding var selection : SelectionValue
    var content : () -> Content

    init(_ label : Label, selection : Binding<SelectionValue>, @ViewBuilder content : @escaping () -> Content) {
        self.label = label
        self._selection = selection
        self.content = content
    }

    private var value: Binding<Int> { Binding<Int>(
        get: {
            Int(selection)
        },
        set: {
            self.selection = SelectionValue($0)
        })
    }

    var body: some View {
        Picker(label, selection: value, content: content)
    }
}

struct CompatibilityPicker_Previews: PreviewProvider {

    struct TestView: View {
        @State var selection : UInt8 = 1
        var body: some View {
            CompatibilityPicker("Difficulty", selection: $selection) {
                Text("Easy").tag(0)
                Text("Normal").tag(1)
                Text("Hard").tag(2)
            }
        }
    }
    static var previews: some View {
        TestView()
    }
}

Swift相关问答推荐

UITableView未显示类数组数据

它是RxSwift中直接用于可扩展视图的有效的可扩展BehaviorRelay值?

在SwiftUI中判断选项时避免重复查看

为什么这个快速代码不显示文本字段?

(Swift)混淆了函数内 for-in 循环的返回值和循环后(函数内)的返回值)

如何将变量添加到不属于可解码部分的 struct ?

如何制作具有 2 个视图的 3D 旋转动画?

数组使用偏移量对自身执行 XOR

故事板未在助手中显示视图控制器

Swift:withCheckedContinuation 和 Dispatch QoSClass

如何在 Swift 中将 XPC 自定义类列入白名单?

在 Swift 4 中实现自定义解码器

我将哪种 Swift 数据类型用于货币

如何在 SwiftUI 中实现 PageView?

在 iOS 中使用 Swift 保存 PDF 文件并显示它们

你如何在 UIBarItem 中使用 setTitleTextAttributes:forState?

如何发送静默推送通知有效负载

在 Swift 中指定 UITextField 的边框半径

来自 ObservableObject 的绑定值

保持窗口始终在顶部?