Quick Answer
Fix incorrect value expressions.
Understanding the Issue
Swift prevents certain values from being used as expressions, especially in statement contexts.
The Problem
This code demonstrates the issue:
Swift
Error
5 + 3 // Warning as standalone statement
The Solution
Here's the corrected code:
Swift
Fixed
// Solution 1: Use result
let sum = 5 + 3
print(sum)
// Solution 2: Wrap in a function
func calculate() -> Int {
return 5 + 3
}
// Solution 3: Use @discardableResult
@discardableResult func add(_ a: Int, _ b: Int) -> Int {
return a + b
}
add(5, 3)
// Solution 4: Make it useful
if 5 + 3 == 8 {
print("Math works")
}
Key Takeaways
Ensure all expressions are used meaningfully or produce side effects.