尝试创建一个根据所选自定义类型返回自定义文本字段的结构

问题描述 投票:0回答:0

尝试创建一个根据所选类型返回自定义文本字段的结构...但是,我收到此错误:

函数声明了一个不透明的返回类型“some View”,但其主体中的返回语句没有匹配的底层类型

我已经使用 Text() 成功完成了此操作,但显然 TextField() 的工作方式有所不同,因为我将接受用户的输入。

见下方代码:

import SwiftUI

/// The types of AtomsTextField's
enum AtomsTextFieldType {
    case roundedBorder
    case `default`
}

/// A struct that returns an AtomsTextField
struct AtomsTextField: View {
    
    /// The text entered into the AtomsTextField
    @Binding var text: String
    /// The AtomsTextField type
    let type: AtomsTextFieldType
    
    /// The body displaying the AtomsTextField
    var body: some View {
        generateAtomsTextField(text: text, type: type)
    }
    
    /// Generates an AtomsTextField according to the type that was passed in
    /// - parameter text: The text the AtomsTextField contains
    /// - parameter type: An AtomsTextFieldType used to determine what AtomsTextField will be returned
    private func generateAtomsTextField(text: String, type: AtomsTextFieldType) -> some View {
        // Returning an AtomsTextField according to the type given
        switch type {
        case .roundedBorder: return TextField("Type here", text: $text).textFieldStyle(.roundedBorder)
        case .default: return TextField("Type here", text: $text)
        }
    }
}

有什么建议为什么以及如何解决它吗?

function swiftui view textfield custom-ui
© www.soinside.com 2019 - 2024. All rights reserved.