-2

I've tried the following code and I got an error.

var possibleNumber = "Hello World!"

if let actualNumber: Int = Int(possibleNumber) {
    print("\"\(possibleNumber)\" has an integer value of \(actualNumber)")
} else {
    print("\"\(possibleNumber)\" could not be converted to an integer")
}

print(actualNumber) // **Here comes the compile time error**

Error:

use of unresolved identifier 'actualNumber'

What is happening behind the object actualNumber? I declared it in the first part of the optional binding: "if let actualNumber: Int ....." then why is it "unresolved"? hmmm... "unresolved" what is that exactly?

I guess printing a nil is somehow wrong, but why it is wrong? I don't know. Can someone explain this a bit more? and BTW is there a really a nil inside the actualNumber? If so, how can I "see" it?

Cœur
  • 37,241
  • 25
  • 195
  • 267
SLN
  • 4,772
  • 2
  • 38
  • 79
  • 2
    the `actualNumber` is only present in the scope it was defined in aka in the scope of the `if`. Outside of the if it simply does not exist, the compiler cannot resolve the name since there is no associated variable defined in that scope. – luk2302 Apr 28 '16 at 10:30
  • @luk2302 Now, I've changed the body of else part to print(actualNumber) is still saids "unresolved...." is that means it still outside of the scope of the "if" – SLN Apr 28 '16 at 10:36
  • 3
    I assume you're using the example code [from the docs on optional binding](https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html#//apple_ref/doc/uid/TP40014097-CH5-ID333). If you continue to read down a bit, you'll see it says "*Constants and variables created with optional binding in an if statement are available only within the body of the if statement.*". You can use a guard statement to use an optional bound variable outside of the scope of the statement. – Hamish Apr 28 '16 at 10:36
  • 1
    Solution: move everything depending on the `actualNumber` inside the `if` branch. The variable is not defined in the `else` branch either - what value should it have? you conditonally bind it in the if-statment, if there is a value present, the if body is exectued, if not then the else body is exectued and the variable is not available. – luk2302 Apr 28 '16 at 10:36
  • you can use `guard let` if you want to use actualNumber after checking. – Samet DEDE Apr 28 '16 at 10:39
  • Thanks all of you, I've take a looked the socpe and optional binding section and have a better understanding now. – SLN Apr 29 '16 at 09:49

3 Answers3

1
you should do something like this :

if let actualNumber = Int(possibleNumber) {
// do something with your number
print("\"\(possibleNumber)\" has an integer value of \(actualNumber)")
} else {
print("\"\(possibleNumber)\" could not be converted to an integer")
}

// you can't access actualNumber outside if

and Int("dd") it will return either nil or optional value.

Sahil
  • 9,096
  • 3
  • 25
  • 29
  • Yes you are right, now I started to know what the scope means from your help, Thanks a lot – SLN Apr 29 '16 at 09:52
1

If scope:

if let actualNumber = Int(possibleNumber) {
    // Here the 'actualNumber' can be accessed because is in scope
} else {
    // Here the 'actualNumber' cannot be accessed because is not in scope 
}

Guard scope

guard let actualNumber = Int(possibleNumber) else {
    // actualNumber cannot be accessed
    // print(actualNumber)
    return
}

// actualNumber can be accessed
print(actualNumber)

But if you want the optional value, you can do something like this

let possibleNumber = "Hello World!"
// actualNumber will be optional (and in this case will be 'nil')
let actualNumber = Int(possibleNumber)

print(actualNumber)
Kisuke
  • 73
  • 1
  • 6
1

You are conditionally declaring a variable in an if-statement and want to use it outside the if-statement. This won't work. First of all, you use conditional chaining where you actualNumber only will be initialized of possibleNumber is not nil. If possibleNumber is nil, actualNumber will not exist.

Secondly, a constant (or variable) declared within an if-statement only has a scope within that if-statement. That means that when you try to access that constant, which you are doing with your last if-statement, the compiler will tell you the constant doesn't exist, "undeclared variable".

Optional chaining is explained by apple on this site.

MacUserT
  • 1,760
  • 2
  • 18
  • 30
  • "If possibleNumber is nil, actualNumber will not exist" is that means if the possibleNumber is nil, the acturalNumber is not even decleared, The precondition for declearing the acturalNumber is the Int(possibleNumber) returns a Int number. – SLN Apr 28 '16 at 11:49
  • Correct. I added a link to optional chaining explained by apple in the answer. – MacUserT Apr 28 '16 at 12:39