简体   繁体   English

如何将 function 中的多个修饰符应用于 SwiftUI 中的给定视图?

[英]How to apply multiple modifier in a function to a given View in SwiftUI?

I have a function with this signature (its funcionality is pretty straightforward I think), the implementation isn't important for us:我有一个带有这个签名的 function(我认为它的功能非常简单),实现对我们来说并不重要:

extension View {
    func border(edge: Edge, color: Color, width: CGFloat = 1, cornerRadius: CGFloat = 0) -> some View
}

Then I want to extend the functionality of the View , and add an extra function that would call the previous function:然后我想扩展View的功能,并添加一个额外的 function 来调用之前的 function:

extension View {
    func border(edges: [Edge], color: Color, width: CGFloat = 1, cornerRadius: CGFloat = 0) -> some View
}

But the first idea I have didn't work.但是我的第一个想法没有奏效。 In UIKit there wouldn't be a problem, but here I cannot figure it out how can I apply multiple (variable quantity) modifier in a single function.在 UIKit 中不会有问题,但在这里我无法弄清楚如何在单个 function 中应用多个(可变数量)修饰符。

I tried this:我试过这个:

extension View {
   func border(edges: [Edge], color: Color, width: CGFloat = 1, cornerRadius: CGFloat = 0) -> some View {
        var view = self
        edges.forEach { edge in
            view = view.border(edge: edge, color: color, width: width, cornerRadius: cornerRadius)
        }
        return view
    }
}

It shows me 2 errors:它向我显示了 2 个错误:

Cannot assign value of type 'some View' to type 'Self'无法将“某些视图”类型的值分配给“自我”类型

Return type of instance method 'border(edges:color:width:cornerRadius:)' requires that 'Self' conform to 'View'实例方法'border(edges:color:width:cornerRadius:)'的返回类型要求'Self'符合'View'

I understand the errors, but I cannot say that let the view variable be a some View ( var view: View = self cannot be compiled).我理解错误,但我不能说让view变量成为some Viewvar view: View = self无法编译)。 How (with what syntax / idea) can I solve this issue?我如何(用什么语法/想法)解决这个问题?

Edit: here is a full code, which shows the issue:编辑:这是一个完整的代码,它显示了问题:

extension SwiftUI.View {
    // not completed implementation, doesn't matter
    public func border(edge: Edge, color: SwiftUI.Color, width: CGFloat = 1, cornerRadius: CGFloat = 0) -> some SwiftUI.View {
        self
    }

    public func border(edges: [Edge], color: SwiftUI.Color, width: CGFloat = 1, cornerRadius: CGFloat = 0) -> some View {
        var view: SwiftUI.View = self
        edges.forEach { edge in
            view = view.border(edge: edge, color: color, width: width, cornerRadius: cornerRadius)
        }
        return view
    }
}

I would solve this task in reverse order - generic implementation for array and single use as array with one element, like:我会以相反的顺序解决这个任务 - 数组的通用实现和单个用作具有一个元素的数组,例如:

extension SwiftUI.View {
    public func border(edge: Edge, color: SwiftUI.Color, width: CGFloat = 1, cornerRadius: CGFloat = 0) -> some SwiftUI.View {
        self.border(edges: [edge], color: color, width: width, cornerRadius: cornerRadius)
    }

    public func border(edges: [Edge], color: SwiftUI.Color, width: CGFloat = 1, cornerRadius: CGFloat = 0) -> some View {
        self  // generic implementation here
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM