Can I use the range operator with if statement in Swift?

If StatementSwiftRange

If Statement Problem Overview


Is it possible to use the range operator ... and ..< with if statement. Maye something like this:

let statusCode = 204
if statusCode in 200 ..< 299 {
  NSLog("Success")
}

If Statement Solutions


Solution 1 - If Statement

You can use the "pattern-match" operator ~=:

if 200 ... 299 ~= statusCode {
    print("success")
}

Or a switch-statement with an expression pattern (which uses the pattern-match operator internally):

switch statusCode {
case 200 ... 299:
    print("success")
default:
    print("failure")
}

Note that ..< denotes a range that omits the upper value, so you probably want 200 ... 299 or 200 ..< 300.

Additional information: When the above code is compiled in Xcode 6.3 with optimizations switch on, then for the test

if 200 ... 299 ~= statusCode

actually no function call is generated at all, only three assembly instruction:

addq	$-200, %rdi
cmpq	$99, %rdi
ja	LBB0_1

this is exactly the same assembly code that is generated for

if statusCode >= 200 && statusCode <= 299

You can verify that with

xcrun -sdk macosx swiftc -O -emit-assembly main.swift

As of Swift 2, this can be written as

if case 200 ... 299 = statusCode {
    print("success")
}

using the newly introduced pattern-matching for if-statements. See also https://stackoverflow.com/questions/30720289/swift-2-pattern-matching-in-if.

Solution 2 - If Statement

This version seems to be more readable than pattern matching:

if (200 ... 299).contains(statusCode) {
    print("Success")
}

Solution 3 - If Statement

This is an old thread, but it seems to me we're over-thinking this. It seems to me the best answer is just

if statusCode >= 200 && statusCode <= 299

There's no

if 200 > statusCode > 299

form that I'm aware of, and the other suggested solutions are doing function calls, which are harder to read, and might be slower to execute. The pattern match method is a useful trick to know, but seems like a poor fit for this problem.

Edit:

Personally, I find the pattern match operator to be hideous, and wish the compiler would support if x in 1...100 syntax. That is sooooo much more intuitive and easy to read than if 1...100 ~= x

Solution 4 - If Statement

I wanted to check 4xx errors except 401. Here is the code:

let i = 401

if 400..<500 ~= i, i != 401 {
    print("yes")
} else {
    print("NO")
}

Solution 5 - If Statement

I preferred Range .contains() operator too, until found that its implementation is inefficient - https://oleb.net/blog/2015/09/swift-ranges-and-intervals/

> We can represent the condition x < 0 using a range: > (Int.min..<0).contains(x) is exactly equivalent. It is vastly slower, > though. The default implementation of contains(_:) traverses the > entire collection, and executing a loop nine quintillion times in the > worst case is not cheap.

Solution 6 - If Statement

if you want to know if the response status code is a success, simply make it this way

  if response.statuscode < 300 {
      print("response is a success")
  }

if you iterate from 200,201,203,204 ... 300. There were just too much unnecessary iterations. Hope this helps! :D

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJimmyView Question on Stackoverflow
Solution 1 - If StatementMartin RView Answer on Stackoverflow
Solution 2 - If StatementSerhii YakovenkoView Answer on Stackoverflow
Solution 3 - If StatementDuncan CView Answer on Stackoverflow
Solution 4 - If StatementabhimuralidharanView Answer on Stackoverflow
Solution 5 - If StatementEntroView Answer on Stackoverflow
Solution 6 - If StatementIrsyad AshariView Answer on Stackoverflow