Result Values in '? :' Expression Have Mismatching Types 'Some View' and '...'

Result values in '? :' expression have mismatching types 'some View' and '...'

The error is telling you that, in an expression:

condition ? true_result : false_result

both true_result and false_result need to have the same type.

There are multiple ways to overcome this, here are two:

.navigationBarItems(leading: searchTapped ? AnyView(backButton) : AnyView(ProfileImageBarButton(showMenu: $showMenu)))

or

.navigationBarItems(leading: barItems())

...

func barItems() -> some View {
return Group {
if searchTapped {
backButton
} else {
ProfileImageBarButton(showMenu: $showMenu)
}
}
}

Result values in '? :' expression have mismatching types '()' and 'Bool'

Note, I don't know Swift, but this doesn't appear to be a Swift specific problem. I can't explain the exact error, but I can show you how to write it properly.

Conditional expressions are used almost exclusively when you need to assign something to a variable or return a value, and have exactly 2 options to choose from.

This is what you're trying to do, but you've written it in a convoluted way that's likely confusing the compiler.

In the expression:

numbers.count > 0 ? deleteAllNumbersButton.isEnabled = true
: deleteAllNumbersButton.isEnabled = false

Because the "then" and "else" expressions both contain assignments, they evaluate (I'm assuming) to a Unit (())/"void". I'm guessing this is why it's yelling at you. It never makes sense to use a ternary to return a Unit (Actually, as noted in the comments, operator precedence is the real reason for the error).

What you likely meant was:

deleteAllNumbersButton.isEnabled = numbers.count > 0 ? true : false

Notice how instead of assigning in the conditional expression, the result of the expression is instead assigned. In cases that can't be simplified further (see below), this is how conditional expressions should be used.

This new form should raise red flags though. Why have the conditional expression evaluate to true/false? That's almost always a code smell. It's redundant given the condition already evaluates to a Boolean value.

Just reduce it down to:

deleteAllNumbersButton.isEnabled = numbers.count > 0

Swift: Result values in '? :' expression have mismatching types 'Class' and '()'

You have this:

let myVariable = mySection.rawValue == 0 ?
myClass.doSomethingAtIndex:(index: indexPath.row) :
myClass.doSomethingElseAtIndex(index: indexPath.row)

and it should be this:

let myVariable = mySection.rawValue == 0 ?
myClass.doSomethingAtIndex(index: indexPath.row) :
myClass.doSomethingElseAtIndex(index: indexPath.row)

Note the ':' that I have removed from between the doSomethingAtIndex and the bracket '('

Why am I getting this error : Result values in '? :' expression have mismatching types '[String]?' and 'String'

This because all of the other properties you are accessing of a placemark are strings, except for the areas of interest, which is an (optional) array of stings. You are trying to cast it when there is no way it can work.

If you really want to format your errors like that, you can just use:

let areaOfInterest = p?.areasOfInterest != nil ? p?.areasOfInterest : ["nil7"]

Result values in '? :' expression have mismatching types 'String.SubSequence' (aka 'Substring') and 'String'

dropFirst() returns a Substring from your string, and the complier is complaining that you return a different Substring type if true and String type when false

Just cast the Substring to String like String(yourSubString) and it should work

var chars = Array(string.hasPrefix("#") ? String(string.characters.dropFirst()) : string.characters)

Result values in '? :' expression have mismatching types '()' and 'String?'

Since you're assigning to self.ddiLabel.text in both of the cases you can separate the assignment from the ?: operation, making the return type String in both cases

ddiLabel.text = account.agent.number == "" || account.agent.ddi == nil ? "02039909000" : account.agent.ddi

SwiftUI ListStyle - Result values in '? :' have mismatching types

They are both list styles, but of different concrete types, so swift type checker does not allow such, instead we can use custom modifier like

extension List {
@ViewBuilder
func insetListStyle(if flag: Bool) -> some View {
if flag {
self.listStyle(InsetGroupedListStyle())
} else {
self // implicit DefaultListStyle
}
}
}

and now use it as

List(subject.tasks, id: \.id) { task in
TaskView(task: task).environmentObject(self.controller)
}
.insetListStyle(if: controller.currentOS == OS.iOS)


Related Topics



Leave a reply



Submit