Nils
Nils

Reputation: 2205

Making a variable from if statement global

While encoding JSON, I´m unwrapping stuff with an if let statement, but I'd like to make a variable globally available

do {
  if
    let json = try JSONSerialization.jsonObject(with: data) as? [String: String], 
    let jsonIsExistant = json["isExistant"] 
  {
    // Here I would like to make jsonIsExistant globally available
  }

Is this even possible? If it isn't, I could make an if statement inside of this one, but I don't think that would be clever or even possible.

Upvotes: 0

Views: 700

Answers (2)

NobodyNada
NobodyNada

Reputation: 7634

It doesn't make sense to expose a variable to the outside of an if let statement:


if let json = ... {
    //This code will only run if json is non-nil.
    //That means json is guaranteed to be non-nil here.
}
//This code will run whether or not json is nil.
//There is not a guarantee json is non-nil.

You have a few other options, depending on what you want to do:


You can put the rest of the code that needs json inside of the if. You said you didn't know if nested if statements are "clever or even possible." They're possible, and programmers use them quite often. You also could extract it into another function:

func doStuff(json: String) {
    //do stuff with json
}

//...
if let json = ... {
    doStuff(json: json)
}

If you know that JSON shouldn't ever be nil, you can force-unwrap it with !:

let json = ...!

You can make the variable global using a guard statement. The code inside of the guard will only run if json is nil. The body of a guard statement must exit the enclosing scope, for example by throwing an error, by returning from the function, or with a labeled break:

//throw an error
do {
    guard let json = ... else {
        throw SomeError
    }
    //do stuff with json -- it's guaranteed to be non-nil here.
}



//return from the function 
guard let json = ... else {
    return
}
//do stuff with json -- it's guaranteed to be non-nil here.



//labeled break
doStuff: do {
    guard let json = ... else {
        break doStuff
    }
    //do stuff with json -- it's guaranteed to be non-nil here.
}

Upvotes: 1

Sethmr
Sethmr

Reputation: 3074

delclare jsonIsExistant at the place you want it. If you are making an iOS App, than above viewDidLoad() create the variable

var jsonIsExistant: String?

then at this point use it

do {
    if let json = try JSONSerialization.jsonObject(with: data) as? [String: String], 
    let tempJsonIsExistant = json["isExistant"] {
        jsonIsExistant = tempJsonIsExistant
    }
}

This could be rewritten like so though

do {
    if let json = try JSONSerialization.jsonObject(with: data) as? [String: String] { 
        jsonIsExistant = json["isExistant"]
    }
} catch {
    //handle error
}

If handled the second way, then you have to check if jsonIsExistant is nil before use, or you could unwrap it immediately with a ! if you are sure it will always have a field "isExistant" every time that it succeeds at becoming json.

Upvotes: 1

Related Questions