简体   繁体   中英

Able to call generic SwiftUI View when in my project but issues once it's in a package and imported

I have a SwiftUI app that calls a View with passed in Views, methods, and variables. This view, let's call it DisplayView is general enough that I'm able to call it in different ways and have it display really different things. This worked great but I recently moved Display View to it's own package and I'm now having trouble calling it.

To create a simple example I started a new app and created a single general view to show how I call it. This WORKS.

// Shared Display View
public struct DisplayView<Content: View>: View {
    // The view we need to display
    let content: Content
    // The methods that need to be executed
    var aMethod: () -> Void
    
    // Boolean
    @Binding var aBoundVariable:Bool
    
    // Regular Variable
    var aName:String
    
    public var body: some View {
            content
#if os(macOS)
                .padding()
#endif
    }
}

Result of this:

当我调用位于我的项目中的通用 DisplayView 时,代码与应用程序结果一起显示。

When I instead move the DisplayView to it's own package and import it I'm having issues calling it.

First the newly formed DisplayView now looks like:


import SwiftUI

// Shared Display View for all the Core Data display views
public struct DisplayView<Content: View>: View {
    // The view we need to display
    let content: Content
    // The methods that need to be executed
    var aMethod: () -> Void
    
    // Boolean
    @Binding var aBoundVariable:Bool
    
    // Regular Variable
    var aName:String
    
    public init(content: any View, aMethod: @escaping () -> Void, aBoundVariable: Binding<Bool>, aName: String) {
        self.content = content as! Content
        self.aMethod = aMethod
        _aBoundVariable = aBoundVariable
        self.aName = aName
    }
    
    public var body: some View {
            content
#if os(macOS)
                .padding()
#endif
    }
}

I then committed,tagged, etc my package and imported it into the first app shown. I commented out the old DisplayView code, imported my package, and attempted to call the new DisplayView .

Code:

import SwiftUI
import thePackage

struct ContentView: View {

    @State var aVariable: Bool = false
    let aName = "hey"
    
    var body: some View {
        DisplayView(content: displayText(), aMethod: randomMethod, aBoundVariable: $aVariable, aName: aName)
    }

    private func randomMethod() {
        print("random method")
    }
    
    @ViewBuilder
    func displayText() -> some View {
        Text("Hello World!")
    }
}

//// Shared Display View --> Now using imported package
//public struct DisplayView<Content: View>: View {
// ...
//}

Just calling the method like before gets this error: Generic parameter 'Content' could not be inferred or image: 错误:无法推断通用参数“内容”

When I hit fix I see a placeholder

DisplayView<<#Content: View#>>(content: displayText(), aMethod: randomMethod, aBoundVariable: $aVariable, aName: aName)

but can't figure out how to use it properly. I've tried several iterations of all sorts including:

View使用视图 some View一些观点 and @ViewBuilder based on other questions I saw online. 和@ViewBuilder 基于我在网上看到的其他问题

I'd love to switch DisplayView into a package so I can use it in several projects but can't figure out how to call it properly when it's a package.

Thanks in advance for any help. :)

You need to change your init to take Content instead of any View then it should work.

import SwiftUI

// Shared Display View for all the Core Data display views
public struct DisplayView<Content: View>: View {
    // The view we need to display
    let content: Content
    // The methods that need to be executed
    var aMethod: () -> Void
    
    // Boolean
    @Binding var aBoundVariable:Bool
    
    // Regular Variable
    var aName:String
    
    public init(content: Content, aMethod: @escaping () -> Void, aBoundVariable: Binding<Bool>, aName: String) {
        self.content = content
        self.aMethod = aMethod
        _aBoundVariable = aBoundVariable
        self.aName = aName
    }
    
    public var body: some View {
            content
#if os(macOS)
                .padding()
#endif
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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