如何在SwiftUI中创建一个返回一些视图并接受闭包的函数?

如何在SwiftUI中创建一个返回一些视图并接受闭包的函数?,swift,swiftui,Swift,Swiftui,我想为视图构建一个函数,它接受一个闭包,比如onChange或onPreferenceChange这是我的代码,用这个代码我可以读取value或value的更改,但我不能将它发送回我的ContentView,因为我想使用print(newValue)内容视图中的。我该怎么做?谢谢你的帮助和时间 所以我们只需要在执行函数中添加回调闭包参数。像这样 func perform(value: String,callback : @escaping (_ value : String ) ->

我想为视图构建一个函数,它接受一个闭包,比如
onChange
onPreferenceChange
这是我的代码,用这个代码我可以读取
value
value
的更改,但我不能将它发送回我的ContentView,因为我想使用
print(newValue)内容视图中的
。我该怎么做?谢谢你的帮助和时间




所以我们只需要在执行函数中添加回调闭包参数。像这样

func perform(value: String,callback : @escaping (_ value : String ) -> Void) -> some View
 
现在我们有了一个回调闭包,在这个闭包中我们可以将值发回

  struct ContentView: View {
            @State private var stringOfText: String = "Hello, world!"
            var body: some View {
                Text(stringOfText)
                    .perform(value: stringOfText) {
                        newValue in
                        print(newValue)
                    }
                Button("update") { stringOfText += " updated!" }.padding()
            }
        }
     
   
   extension View {
        func perform(value: String,callback : @escaping (_ value : String ) -> Void) -> some View {
            return self
                .preference(key: CustomPreferenceKey.self, value: value)
                .onPreferenceChange(CustomPreferenceKey.self) { newValue in
                    callback(newValue)
                }
        }
    }
    
    struct CustomPreferenceKey: PreferenceKey {
        static var defaultValue: String { get { return String() } } 
        static func reduce(value: inout String, nextValue: () -> String) { value = nextValue() }
        
    }

onChange
不会“返回闭包”。你的意思是接受闭包的函数吗?根本不清楚你想做什么。你能提供一个使用这个的例子吗?@Sweeper:是的,对不起。我刚打错了,但是更新了!不鼓励只使用代码的答案,请添加代码作用的解释感谢您指出@JoakimDanielson。我补充了解释。
struct CustomPreferenceKey: PreferenceKey {
    
    static var defaultValue: String { get { return String() } }
    
    static func reduce(value: inout String, nextValue: () -> String) { value = nextValue() }
    
}
func perform(value: String,callback : @escaping (_ value : String ) -> Void) -> some View
 
  struct ContentView: View {
            @State private var stringOfText: String = "Hello, world!"
            var body: some View {
                Text(stringOfText)
                    .perform(value: stringOfText) {
                        newValue in
                        print(newValue)
                    }
                Button("update") { stringOfText += " updated!" }.padding()
            }
        }
     
   
   extension View {
        func perform(value: String,callback : @escaping (_ value : String ) -> Void) -> some View {
            return self
                .preference(key: CustomPreferenceKey.self, value: value)
                .onPreferenceChange(CustomPreferenceKey.self) { newValue in
                    callback(newValue)
                }
        }
    }
    
    struct CustomPreferenceKey: PreferenceKey {
        static var defaultValue: String { get { return String() } } 
        static func reduce(value: inout String, nextValue: () -> String) { value = nextValue() }
        
    }