4

Anyone know if SwiftUI support ternary conditionals? I have a text view with conditional argument (Text(badgeCount == nil ? " " :"\(badgeCount!)")) that displays an empty view. Surprisingly, it works if I remove the @State attribute from the view.

import SwiftUI
struct RedBadgeView: View {
 @State var badgeCount: Int?
 
 init (_ badgeCount: Int? = nil) {
 self.badgeCount = badgeCount
 }
 
 var body: some View {
 // Something about this Syntax is throwing off SwiftUI. 
 Text(badgeCount == nil ? " " :"\(badgeCount!)")
 }
}
struct RedBadgeView_Previews: PreviewProvider {
 static var previews: some View {
 RedBadgeView(1)
 }
}
asked Nov 11, 2020 at 16:18
0

3 Answers 3

4

There's no need to use the ternary operator here at all. Moreover, doing a nil check and then force unwrapping in a ternary operator is a bad idea.

Instead, you should use optional chaining to access the description of badgeCount and provide the " " as a default value.

Text(badgeCount?.description ?? " ")

However, your problem of the view not updating is coming from the fact that you never initialise your State, you just assign a value to its wrapped value. To access the state, you need to use the _ prefix before the variable name.

init (_ badgeCount: Int? = nil) {
 self._badgeCount = State(initialValue: badgeCount)
}
answered Nov 11, 2020 at 16:34
Sign up to request clarification or add additional context in comments.

Comments

3

Sure ternary conditions work. However you initialization of the State is wrong. You are initializing a regular Int.

init (_ badgeCount: Int? = nil) {
 self.badgeCount = badgeCount // >> If you initializing an Int
 self._badgeCount = State(initialValue: badgeCount) // >> If you initializing a State variable
}

Hence, everything will work with State aswell:

struct ContentView: View {
 @State var badgeCount: Int?
 
 init (_ badgeCount: Int? = nil) {
 self._badgeCount = State(initialValue: badgeCount)
 }
 
 var body: some View {
 Text(badgeCount == nil ? " " :"\(badgeCount!)")
 }
}
answered Nov 11, 2020 at 16:25

Comments

1

In addition to other answers, if you're using SwiftUI 2 you can do it in more swifty way which is to use if-let directly in the body:

var body: some View {
 if let badgeCount = badgeCount {
 Text(badgeCount)
 }
}

If badgeCount is nil, the body will return an EmptyView. However, you may return some other view as well:

@ViewBuilder
var body: some View {
 if let badgeCount = badgeCount {
 Text(badgeCount)
 } else {
 Text(" ")
 }
}

(First you need to init/assign a value to badgeCount to see the view).

answered Nov 11, 2020 at 17:48

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.