Close Menu
  • Home
  • AI
  • Big Data
  • Cloud Computing
  • iOS Development
  • IoT
  • IT/ Cybersecurity
  • Tech
    • Nanotechnology
    • Green Technology
    • Apple
    • Software Development
    • Software Engineering

Subscribe to Updates

Get the latest technology news from Bigteetechhub about IT, Cybersecurity and Big Data.

    What's Hot

    Firmware-Managed Verified Boot with Hardware, Building Trust From Power-On

    July 16, 2025

    Scientists discover the moment AI truly understands language

    July 16, 2025

    Zero Trust Explained: Why Traditional

    July 16, 2025
    Facebook X (Twitter) Instagram
    Facebook X (Twitter) Instagram
    Big Tee Tech Hub
    • Home
    • AI
    • Big Data
    • Cloud Computing
    • iOS Development
    • IoT
    • IT/ Cybersecurity
    • Tech
      • Nanotechnology
      • Green Technology
      • Apple
      • Software Development
      • Software Engineering
    Big Tee Tech Hub
    Home»iOS Development»Ternary operator in Swift explained – Donny Wals
    iOS Development

    Ternary operator in Swift explained – Donny Wals

    big tee tech hubBy big tee tech hubJuly 8, 2025003 Mins Read
    Share Facebook Twitter Pinterest Copy Link LinkedIn Tumblr Email Telegram WhatsApp
    Follow Us
    Google News Flipboard
    Ternary operator in Swift explained – Donny Wals
    Share
    Facebook Twitter LinkedIn Pinterest Email Copy Link


    The ternary operator is one of those things that will exist in virtually any modern programming language. When writing code, a common goal is to make sure that your code is succinct and no more verbose than it needs to be. A ternary expression is a useful tool to achieve this.

    What is a ternary?

    Ternaries are essentially a quick way to write an if statement on a single line. For example, if you want to tint a SwiftUI button based on a specific condition, your code might look a bit as follows:

    struct SampleView: View {
      @State var username = ""
    
      var body: some View {
        Button {} label: {
          Text("Submit")
        }.tint(username.isEmpty ? .gray : .red)
      }
    }

    The line where I tint the button contains a ternary and it looks like this: username.isEmpty ? .gray : .red. Generally speaking, a ternary always has the following shape ? : . You must always provide all three of these “parts” when using a ternary. It’s basically a shorthand way to write an if {} else {} statement.

    When should you use ternaries?

    Ternary expressions are incredibly useful when you’re trying to assign a property based on a simple check. In this case, a simple check to see if a value is empty. When you start nesting ternaries, or you find that you’re having to evaluate a complex or long expression it’s probably a good sign that you should not use a ternary.

    It’s pretty common to use ternaries in SwiftUI view modifiers because they make conditional application or styling fairly straightforward.

    That said, a ternary isn’t always easy to read so sometimes it makes sense to avoid them.

    Replacing ternaries with if expressions

    When you’re using a ternary to assign a value to a property in Swift, you might want to consider using an if / else expression instead. For example:

    let buttonColor: Color = if username.isEmpty { .gray } else { .red }

    This syntax is more verbose but it’s arguably easier to read. Especially when you make use of multiple lines:

    let buttonColor: Color = if username.isEmpty { 
      .gray 
    } else {
      .red
    }

    For now you’re only allowed to have a single expression on each codepath which makes them only marginally better than ternaries for readability. You also can’t use if expressions everywhere so sometimes a ternary just is more flexible.

    I find that if expressions strike a balance between evaluating longer and more complex expressions in a readable way while also having some of the conveniences that a ternary has.



    Source link

    Donny Explained Operator Swift Ternary Wals
    Follow on Google News Follow on Flipboard
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email Copy Link
    tonirufai
    big tee tech hub
    • Website

    Related Posts

    Zero Trust Explained: Why Traditional

    July 16, 2025

    Four Months in the Making: SwiftMCP 1.0 is Here

    July 15, 2025

    Ultimate UICollectionView guide with iOS examples written in Swift

    July 14, 2025
    Add A Comment
    Leave A Reply Cancel Reply

    Editors Picks

    Firmware-Managed Verified Boot with Hardware, Building Trust From Power-On

    July 16, 2025

    Scientists discover the moment AI truly understands language

    July 16, 2025

    Zero Trust Explained: Why Traditional

    July 16, 2025

    Running high-performance PostgreSQL on Azure Kubernetes Service

    July 16, 2025
    Advertisement
    About Us
    About Us

    Welcome To big tee tech hub. Big tee tech hub is a Professional seo tools Platform. Here we will provide you only interesting content, which you will like very much. We’re dedicated to providing you the best of seo tools, with a focus on dependability and tools. We’re working to turn our passion for seo tools into a booming online website. We hope you enjoy our seo tools as much as we enjoy offering them to you.

    Don't Miss!

    Firmware-Managed Verified Boot with Hardware, Building Trust From Power-On

    July 16, 2025

    Scientists discover the moment AI truly understands language

    July 16, 2025

    Subscribe to Updates

    Get the latest technology news from Bigteetechhub about IT, Cybersecurity and Big Data.

      • About Us
      • Contact Us
      • Disclaimer
      • Privacy Policy
      • Terms and Conditions
      © 2025 bigteetechhub.All Right Reserved

      Type above and press Enter to search. Press Esc to cancel.