"Generic Parameter Could Not Be Inferred" in Swiftui Uiviewrepresentable

Generic parameter could not be inferred in SwiftUI UIViewRepresentable

Here is possible approach that allows usage of provided ContentView as-is.

Just change the direction of... instead of making entire type generic, which is actually not needed in this case, just make a generic only initialisation, like below.

Also it actually makes clear that Action is Content-independent, that is really correct.

Tested & works with Xcode 11.2 / iOS 13.2 (w/o no changes in ContentView)

struct LegacyScrollView: UIViewRepresentable {
enum Action {
case idle
case offset(x: CGFloat, y: CGFloat, animated: Bool)
}

@Binding var action: Action
private let uiScrollView: UIScrollView

init<Content: View>(content: Content) {
let hosting = UIHostingController(rootView: content)
hosting.view.translatesAutoresizingMaskIntoConstraints = false

uiScrollView = UIScrollView()
uiScrollView.addSubview(hosting.view)

let constraints = [
hosting.view.leadingAnchor.constraint(equalTo: uiScrollView.leadingAnchor),
hosting.view.trailingAnchor.constraint(equalTo: uiScrollView.trailingAnchor),
hosting.view.topAnchor.constraint(equalTo: uiScrollView.contentLayoutGuide.topAnchor),
hosting.view.bottomAnchor.constraint(equalTo: uiScrollView.contentLayoutGuide.bottomAnchor),
hosting.view.widthAnchor.constraint(equalTo: uiScrollView.widthAnchor)
]
uiScrollView.addConstraints(constraints)

self._action = Binding.constant(Action.idle)
}

init<Content: View>(@ViewBuilder content: () -> Content) {
self.init(content: content())
}

init<Content: View>(action: Binding<Action>, @ViewBuilder content: () -> Content) {
self.init(content: content())
self._action = action
}

func makeCoordinator() -> Coordinator {
Coordinator(self)
}

func makeUIView(context: Context) -> UIScrollView {
return uiScrollView
}

func updateUIView(_ uiView: UIScrollView, context: Context) {
switch self.action {
case .offset(let x, let y, let animated):
uiView.setContentOffset(CGPoint(x: x, y: y), animated: animated)
DispatchQueue.main.async {
self.action = .idle
}
default:
break
}
}

class Coordinator: NSObject {
let legacyScrollView: LegacyScrollView

init(_ legacyScrollView: LegacyScrollView) {
self.legacyScrollView = legacyScrollView
}
}

}

Generic parameter 'Content' could not be inferred SwiftUI

You should not declare an extra generic parameter Content in the initialiser. The initialiser should not be generic, and instead just use the Content generic parameter from PersonView:

extension PersonView {
init(person: Person, content: (() -> Content)? = nil) {
self.init(text: Text(person.name),
detailText: Text("\(person.age)"), content: content)
}
}

The extra Content that you declared is a different generic parameter from the Content in PersonView<Content>, which is why the compiler says it can't convert the types.

SwiftUI: Generic parameter 'Subject' could not be inferred

Since your LoadingView is not going to modify .isLoading, you do not need to pass it as a binding:

LoadingView(isShowing: self.$charStore.isLoading)

Instead, remove the @Binding in LoadingView:

struct LoadingView<Content>: View where Content: View {

var isShowing: Bool
...

and create it like this (remove the dollar sign):

LoadingView(isShowing: self.charStore.isLoading) { ... }

On the contrary, if you insist on passing a binding, then you need to remove the private(set) from:

@Published private(set) var isLoading = false

SwiftUI: Generic parameter 'Value' could not be inferred

Your bottomMenu function has an extra generic in its signature. Change it to:

fileprivate func bottomMenu<Content>(

(Note that Value from your original, which is unused, is removed)

SwiftUI error: Generic parameter 'Label' could not be inferred Explicitly specify the generic arguments to fix this issue `

This is a case of nesting closures interfering with $0. Try this:

self.book.map { book in Button(action: { self.onRemoveCard(card, fromBook: book) }) { ... }

SwiftUI Error Code Generic parameter 'C0' could not be inferred

This line is incorrect:

EliminationModel.printTest()

printTest is an instance method:

EliminationModel().printTest()

The CGFloat(0)! references are not legal, since CGFloat(0) is not optional. (There's also no need for the CGFloat() in any of this code.)

.frame(minWidth: 0)!, maxWidth: .infinity, minHeight: CGFloat(0)!, maxHeight: .infinity, alignment: Alignment.topLeading)

This line should be:

.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .topLeading)

(Though I'm not sure you need to set minWidth and minHeight to 0 here.)


This modifier is not attached to anything; it seems to be a typo:

.textFieldStyle(RoundedBorderTextFieldStyle())

And another incorrect CGFloat(Int)!:

.font(Font.custom("Roboto Mono", size: CGFloat(20)!))

This should be:

.font(Font.custom("Roboto Mono", size: 20))

The way I debugged this was by repeatedly using Cmd-Shift-A>Extract Subview to make the Views smaller. Having one massive View is very hard for the compiler to type-check, and it tends to point you to the wrong line.

Putting it all back together is this (but I recommend refactoring it into smaller views.)

struct ContentView: View {

var eliminationModel = EliminationModel()
@State private var code: String = "0000"
let roboto = UIFont(name: "Roboto Mono", size: UIFont.systemFontSize)


var body: some View {
ZStack() {
Image("background")
.resizable()
.edgesIgnoringSafeArea(/*@START_MENU_TOKEN@*/.all/*@END_MENU_TOKEN@*/)
VStack() {
HStack() {
VStack() {
Text("CODE:")
.font(Font.custom("Roboto Mono", size: 15))
.multilineTextAlignment(.center)
Text("6969")
.font(Font.custom("Roboto Mono", size: 10))
.padding(.top, 10.0)

Button(action: {
EliminationModel().printTest()
}) {
Text("Button")
}
}

.padding(.horizontal, 50)

VStack() {

// FIXME: Unclear what this is supposed to be part of
// .textFieldStyle(RoundedBorderTextFieldStyle())
Text("INPUT: \(code)")
.font(Font.custom("Roboto Mono", size: 20))

}
}
.padding([.top, .leading, .trailing], 20.0)
Spacer()
HStack() {
VStack() {
Text("YOUR TARGET IS: ")
.font(Font.custom("Roboto Mono", size: 25))
.padding(.bottom, 11.0)
Text("JOHN APPLESEED")
.font(Font.custom("Roboto Mono", size: 40))
.fontWeight(.black)
.foregroundColor(Color.gray)
.multilineTextAlignment(.center)
Image("headshot")
.cornerRadius(10.0)
}
}//hs
Spacer()

HStack() {
VStack(alignment: .center) {
Text("GRADE: ")
.font(Font.custom("Roboto Mono", size: 15))
.multilineTextAlignment(.center)
Text("FACULTY")
.font(Font.custom("Roboto Mono", size: 20))
}
VStack(alignment: .center) {
Text("DORM: ")
.font(Font.custom("Roboto Mono", size: 15))
.multilineTextAlignment(.center)
Text("DAY")
.font(Font.custom("Roboto Mono", size: 20))

}
} //hs
Spacer()

}

.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .topLeading)
} // zstack
} // body view
} // struct

SwiftUI - trying to make codable enums but get error: Generic parameter 'S' could not be inferred

The problem is in your decoding method. You need to first decode your typeOfSalary key which is a String, switch the decoded String and decode the percentage in case it is equal to TypeOfSalaryCodingKeys.upTo.rawValue:

init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let typeOfSalary = try container.decode(String.self, forKey: .typeOfSalary)
switch typeOfSalary {
case TypeOfSalaryCodingKeys.noSalary.rawValue:
self = .noSalary
case TypeOfSalaryCodingKeys.upTo.rawValue:
self = .upTo(percentage: try container.decode(Int.self, forKey: .percentage))
default:
throw DecodingError.dataCorruptedError(forKey: CodingKeys.typeOfSalary, in: container, debugDescription: "Invalid Type of Salary: \(typeOfSalary)")
}
}

Playground testing:

let ps1: ParentalSalary = .noSalary
let ps2: ParentalSalary = .upTo(percentage: 27)
let parentalSalaries: [ParentalSalary] = [ps1,ps2]

do {
let psData = try JSONEncoder().encode(parentalSalaries)
print(String(data: psData, encoding: .utf8)!)
let loadedJSON = try JSONDecoder().decode([ParentalSalary].self, from: psData)
print(loadedJSON)
} catch {
print(error)
}

This will print:

[{"typeOfSalary":"noSalary"},{"typeOfSalary":"upTo","percentage":27}]
[__lldb_expr_1.ParentalSalary.noSalary,
__lldb_expr_1.ParentalSalary.upTo(percentage: 27)]



Related Topics



Leave a reply



Submit