10 Swift Programming Tips — ONE Liners

Imad Ali Mohammad
3 min readApr 27, 2023

--

Swift Programming

Swift is an incredibly powerful and expressive programming language that enables developers to write concise and elegant code. In this article, we’ll dive into 10 one-liners that demonstrate Swift’s capabilities and make you look like an expert in the process. Let’s get started!

#1 Ternary Condition Operator:

The ternary operator allows you to run a check and return one of two values depending on the result of that check — it has the name “ternary” because it works with three values rather than two or one like other operators. You’ll often see it written as ?: but in practice the ? and : are used separately.

let score = 48
let result = score > 45 ? "Pass" : "Fail"

In the above line of code, the result contains the boolean true as the score is greater than 45

#2 Swapping the values without the temp variable:

Swift makes it incredibly easy to swap the values of two variables without using a temporary variable. Using tuple destructuring, you can swap values in just one line:

var a = 7
var b = 9
(a, b) = (b, a)
print(a) // Output: 9
print(b) // Output: 7

#3 Check if a Word Exists in a Sentence:

You can check if a particular word exists in a string with a simple one-liner:

let favorites = ["Banana", "Orange", "Apple"] 
let bag = "I packed some Veggies, Potatoes, and a Banana"
let hasFavorite = !favorites.filter({bag.contains($0)}).isEmpty
print(hasFavorite)

Output:
true

#4 Short-circuit optional unwrapping:

In Swift, you can unwrap an optional value and execute a closure if the value is not nil. This can be achieved in just one line using optional chaining:

let optionalValue: Int? = 99
optionalValue.map { print("The value is: \($0)") } // Output: The value is: 99

#5 Filtering an array with a single condition:

You can filter an array based on a single condition in just one line using the filter function:

let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
let evenNumbers = numbers.filter { $0 % 2 == 0 }
print(evenNumbers) // Output: [2, 4, 6, 8, 10]

#6 Converting an array of strings to uppercase:

Swift’s mapfunction allows you to apply a transformation to each element in an array. This one-liner converts all strings in an array to uppercase:

let names = ["Alice", "Bob", "James"]
let uppercasedNames = names.map { $0.uppercased() }
print(uppercasedNames) // Output: ["ALICE", "BOB", "JAMES"]

#7 Summing an array of integers:

Swift’s reduce function allows you to accumulate a single value by successively combining elements in an array. This one-liner calculates the sum of an array of integers:


let numbers = [1, 2, 3, 4, 5]
let sum = numbers.reduce(0, +)
print(sum) // Output: 15

#8 Checking if all elements in an array meet a condition:

Swift’s allSatisfy function allows you to determine if all elements in an array meet a specified condition. This one-liner checks if all elements in an array are even:

let numbers = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
let allEven = numbers.allSatisfy { $0 % 2 == 0 }
print(allEven) // Output: true

#9 Joining an array of strings with a separator

Swift makes it easy to join an array of strings with a separator using the joined function. This one-liner joins an array of strings with a comma and space:

let address = ["123 Abc", "My Street", "Kingston", "New York"]
let fullAddress = address.joined(separator: ", ")
print(fullAddress) // Output: "123 Abc, My Street, Kingston, New York"

#10 Creating a dictionary from two arrays

You can create a dictionary from two arrays (keys and values) using the zip function and a dictionary initializer. This one-liner creates a dictionary from two arrays:

let keys = ["a", "b", "c"]
let values = [1, 2, 3]
let keyValuePairs = Dictionary(uniqueKeysWithValues: zip(keys, values))
print(keyValuePairs) // Output: ["a": 1, "b": 2, "c": 3]

These nine one-liners showcase the power and expressiveness of the language. With these one-liners in your arsenal, you can write concise and elegant code that demonstrates your expertise in Swift programming. Keep practicing and exploring more advanced Swift techniques to further expand your skillset and elevate your code to the next level.

I hope you enjoyed this article, Happy Coding :-)

--

--

Imad Ali Mohammad
Imad Ali Mohammad

Written by Imad Ali Mohammad

Senior iOS Engineer, Passionate about coding and building amazing apps.

Responses (1)