8

In Swift2, you could have something similar to the following code:

    switch productIdentifier {
    case hasSuffix("q"):
        return "Quarterly".localized
    case hasSuffix("m"):
        return "Monthly".localized
    default:
        return "Yearly".localized
    }

and it would work. In Swift 3, the only way I can make the above work is:

    switch productIdentifier {
    case let x where x.hasSuffix("q"):
        return "Quarterly".localized
    case let x where x.hasSuffix("m"):
        return "Monthly".localized
    default:
        return "Yearly".localized
    }

which seems to lose the clarity of the Swift2 version - and it makes me think I'm missing something. The above is a simple version of course. I'm curious if anyone has a better way of handling that?

7
  • 1
    I cannot make your Swift 2 code work on Swift 2.2.1/Xcode 7.3.1. Could you please show us an actually working code? Commented Oct 23, 2016 at 7:23
  • That was working code - I took straight from my codebase. However some things like the extension on Strings for localized wouldn't work for you because you don't have that extension. Commented Aug 14, 2017 at 23:55
  • It would be better if you could have show such missing parts. You were just lucky that you could have gotten a right answer in a short period, such lack of info would lead flooding low quality answers. Commented Aug 15, 2017 at 0:43
  • It was not relevant. So no. The only relevant part was the switch and case. The return value was not. The question was specifically on the case statement and that was obvious. Commented Aug 15, 2017 at 5:10
  • If it was really obvious, I would not have written such a comment. Something obvious in your mind may not be obvious for readers. Always think in that way when posting a question, and you can improve the question and you may get right answer sooner. Answerers just had ignored your Swift 2 code as it did not make sense. Commented Aug 15, 2017 at 5:15

2 Answers 2

24

I don't know if this is any better than using value binding as in your example, but you can just use an underscore instead,

switch productIdentifier {
case _ where productIdentifier.hasSuffix("q"):
    return "Quarterly".localized
case _ where productIdentifier.hasSuffix("m"):
    return "Monthly".localized
default:
    return "Yearly".localized
Sign up to request clarification or add additional context in comments.

1 Comment

Appreciate that. I just feel wrong reiterating the variable name on each case.
3

You seem to be only checking the last character of the productIdentifier. You could do it this way:

switch productIdentifier.last {
case "q"?:
    return "Quarterly".localized
case "m"?:
    return "Monthly".localized
default:
    return "Yearly".localized
}

4 Comments

This is a better solution to this specific problem, +1
@AlexanderMomchliov, agreed. I personally don't think the Swift 3 syntax is that unclear, but I would use this solution if I were only checking the last character.
This would work. I did typically stay away from the characters collection for performance reasons as a general rule. It also removes any optimization’s the standard library has. So as a practical matter the original code is better, though not cleaner looking.
I would think that obtaining the last character of the string once would be more efficient than repeatedly applying hasSuffix. In either case, clarity for the reader is probably the bigger concern. In Swift4 the syntax becomes productIdentifier.last.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.