Swift switch and if statements should be expressions
| Originator: | GriotSpeak | ||
| Number: | rdar://17429654 | Date Originated: | 2014-06-23 |
| Status: | Closed as Duplicate of 16169366 (Open) | Resolved: | |
| Product: | Product Version: | ||
| Classification: | Reproducible: |
Summary:
There are some situations where the conditions determining a a constant value are more complicated than can be represented in ternary operator, do not merit a separate function call, and would benefit from remaining non-optional constants instead of variables or optionals.
Steps to Reproduce:
func ordinalSuffix(num:Int) -> String {
let abs_number = abs(num)
let rem10 = abs_number % 10
let rem100 = abs_number % 100
if (rem100 == 11 ||
rem100 == 12 ||
rem100 == 13) { /** cannot use range object*/
return "th"
} else if rem10 == 1 {
return "st"
} else if rem10 == 2 {
return "nd"
} else if rem10 == 3 {
return "rd"
} else {
return "th"
}
}
Expected Results:
func ordinalSuffix(num:Int) -> String {
let abs_number = abs(num)
let rem10 = abs_number % 10
let rem100 = abs_number % 100
let suffixString = if (rem100 == 11 ||
rem100 == 12 ||
rem100 == 13) {
"th"
} else if rem10 == 1 {
"st"
} else if rem10 == 2 {
"nd"
} else if rem10 == 3 {
"rd"
} else {
"th"
}
return suffixString
}
Actual Results:
only the first example works
Version:
Notes:
Configuration:
Attachments:
Comments
Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!