Logo

dev-resources.site

for different kinds of informations.

Directly store value using "if expression" - Swift

Published at
12/4/2024
Categories
swift
ios
Author
annurdien
Categories
2 categories in total
swift
open
ios
open
Author
9 person written this
annurdien
open
Directly store value using "if expression" - Swift

When we want to set a variable for a specific condition, this what we normally do:

let temperatureInCelsius = 25
let weatherAdvice: String

if temperatureInCelsius <= 0 {
    weatherAdvice = "It's very cold. Consider wearing a scarf."
} else if temperatureInCelsius >= 30 {
    weatherAdvice = "It's really warm. Don't forget to wear sunscreen."
} else {
    weatherAdvice = "It's not that cold. Wear a T-shirt."
}

print(weatherAdvice)
// Prints "It's not that cold. Wear a T-shirt."
Enter fullscreen mode Exit fullscreen mode

We can make it more cleaner using if expression:

let temperatureInCelsius = 25
let weatherAdvice = if temperatureInCelsius <= 0 {
    "It's very cold. Consider wearing a scarf."
} else if temperatureInCelsius >= 30 {
    "It's really warm. Don't forget to wear sunscreen."
} else {
    "It's not that cold. Wear a T-shirt."
}

print(weatherAdvice)
// Prints "It's not that cold. Wear a T-shirt."
Enter fullscreen mode Exit fullscreen mode

Learn more:

https://docs.swift.org/swift-book/documentation/the-swift-programming-language/controlflow#Conditional-Statements

swift Article's
30 articles in total
Favicon
MVVM directory structure for larger project
Favicon
What Do All iOS Engineers Keep Forgetting?
Favicon
Mastering 4 way Infinite Scroll in SwiftUI!
Favicon
iOS Background Modes: A Quick Guide
Favicon
Debugging in Xcode: Tips to Save Your Time πŸ› οΈ
Favicon
I created a cool SwiftUI library!
Favicon
Optimizing iOS App Performance
Favicon
The Ultimate Guide to iOS Development: Closures (Part 7)
Favicon
Boost Your App’s Performance with Lazy Stacks in SwiftUI
Favicon
The Ultimate Guide to iOS Development: Collections (Part 6)
Favicon
Access Control Levels in Swift
Favicon
Meet swift-api-client
Favicon
The Ultimate Guide to iOS Development: Functions (Part 5)
Favicon
Unlocking Face ID Integration: Boost Security and User Experience πŸš€
Favicon
Unlocking Business Growth with iOS Development Services
Favicon
MVC vs MVVM: A Real-Life iOS Interview Insight
Favicon
The Ultimate Guide to iOS Development: Control Flow (Part 4)
Favicon
Swift: A Journey into Open Source Excellence
Favicon
Object-Oriented Programming inΒ Swift
Favicon
A Drum machine for iOS
Favicon
The Ultimate Guide to iOS Development: Variables, Data Types, and Basic Operations in Swift (Part 3)
Favicon
The Ultimate Guide to iOS Development: From Programming Basics to Building Your First App (Part 2)
Favicon
Top 5 iOS App Templates to Kickstart Your Next Project
Favicon
The Ultimate Guide to iOS Development: From Programming Basics to Building Your First App (Part 1)
Favicon
Cosmos is hiring a senior iOS eng
Favicon
How To Add Multiple Modules In The Swift Package Manager
Favicon
Enhance Debugging in Swift with #file, #line, and #function
Favicon
Directly store value using "if expression" - Swift
Favicon
Day 3: Mull It Over | Advent of Code 2024 | Swift | δΈ­ζ–‡
Favicon
Property Wrappers in Swift: From Basics to Advanced Techniques

Featured ones: