0

I can't figure out why this 'optional' isn't working in scenario 1, but without the optional ? it works in scenario 2.

Using Swift v 1.2, xCode 6.2

var stuff = "6t"

// SCENARIO 1
// Why is this failing when stuff contains non-digit characters?
// i.e. it works if stuff = "45".
if let value: Int? = stuff.toInt() {
    println("value \(value!)")
}

// SCENARIO 2    
// This works!
if let value = stuff.toInt() {
    println("val3 \(value)")
}

For Reference also see these SO Answers: * I wonder if the Sift 1.2 example/Answer here is just plain wrong? Swift - Converting String to Int

Converting String to Int in Swift

Community
  • 1
  • 1
Giant Elk
  • 5,375
  • 9
  • 43
  • 56
  • 2
    `if let value: Int?` makes no sense — the whole point of `if let` is to unwrap an optional – Aaron Brager Jul 01 '16 at 22:27
  • 3
    Why would anyone be using Swift 2.1? Swift 2 has been out for ages, and Swift 3 is here already! – matt Jul 01 '16 at 22:47
  • Swift 1.2 @matt - because I can't upgrade xCode without upgrading my OS which will mess up my ProTools installation. – Giant Elk Jul 02 '16 at 17:54
  • The way I solve that is with an external Thunderbolt SSD. I've installed El Capitan on that, along with Xcode 7 and Xcode 8. I boot off that when I want to do Xcode. Meanwhile, back on the internal drive, the old system is still there. Come on, you need to take a little responsibility for what version of Swift you're using! – matt Jul 02 '16 at 17:59
  • Believe me, it's on my todo list, was hoping to run new OS X in a virtual machine. I use ThunderBolt output for my 2nd monitor, could use USB, but prefer VM if it performs OK. – Giant Elk Jul 05 '16 at 02:10

2 Answers2

1

There is no point of using if let value: Int?. If the if let works, then the value is an Int. There is no way that it could be nil. Therefore, you do not need to declare it as an optional.

Pranav Wadhwa
  • 7,666
  • 6
  • 39
  • 61
1

The first IF is always true.

Infact in both cases:

  1. when the toInt() returns a valid Int
  2. when returns nil

the if let will succeed (and yes, the first IF is useless).

Specifically in your code toInt() returns nil in both scenarios. But in your first scenario you are simply accepting nil as a valid value to enter the THEN block.

Luca Angeletti
  • 58,465
  • 13
  • 121
  • 148
  • I'm still confused. Are you saying this expression is true: `if let value: Int? = nil` – Giant Elk Jul 02 '16 at 18:00
  • In a Playground this evaluates to `nil`, so I would of thought the if statement would fail: ` var stuff = "6r" let value: Int? = stuff.toInt() ` – Giant Elk Jul 02 '16 at 18:13
  • 1
    Now I see what `let` does. This works because it's declaring the optional before the `let`: `var result: Int? = stuff.toInt() if let value = result { println("value \(value)") }` – Giant Elk Jul 02 '16 at 18:45
  • @GiantElk: exactly. Since `value` is declared as `Int?` and `toInt()` always returns an `Int?` (sometimes an `Int` sometimes a `nil`), the assignment will always succeed. – Luca Angeletti Jul 02 '16 at 18:48