Checking for integer multiples
About 2 min
Checking for integer multiples 관련
HACKING WITH SWIFT
What's new in Swift?
Checking for integer multiples | Changes in Swift 5.0
Checking for integer multiples
Available from Swift 5.0
SE-0225 (apple/swift-evolution
) adds an isMultiple(of:)
method to integers, allowing us to check whether one number is a multiple of another in a much clearer way than using the division remainder operation, %
.
For example:
let rowNumber = 4
if rowNumber.isMultiple(of: 2) {
print("Even")
} else {
print("Odd")
}
Yes, we could write the same check using if rowNumber % 2 == 0
but you have to admit that’s less clear – having isMultiple(of:)
as a method means it can be listed in code completion options in Xcode, which aids discoverability.
Other Changes in Swift 5.0
Raw strings | Changes in Swift 5.0
Raw strings
A standard Result type | Changes in Swift 5.0
A standard Result type
Customizing string interpolation | Changes in Swift 5.0
Customizing string interpolation
Dynamically callable types | Changes in Swift 5.0
Dynamically callable types
Handling future enum cases | Changes in Swift 5.0
Handling future enum cases
Flattening nested optionals resulting from try? | Changes in Swift 5.0
Flattening nested optionals resulting from try?
Transforming and unwrapping dictionary values with compactMapValues() | Changes in Swift 5.0
Transforming and unwrapping dictionary values with compactMapValues()