简体   繁体   English

从if语句全局设置变量

[英]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 在编码JSON时,我使用if let语句来展开内容,但我想使变量全局可用

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. 如果不是,我可以在该语句中做一个if语句,但是我认为那不是聪明的,甚至是不可能的。

delclare jsonIsExistant at the place you want it. 在您需要的位置添加delclare jsonIsExistant。 If you are making an iOS App, than above viewDidLoad() create the variable 如果您要制作iOS App,请在viewDidLoad()之上创建变量

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 ! 如果以第二种方式处理,则必须在使用前检查jsonIsExistant是否为nil,否则可以立即用!对其进行包装。 if you are sure it will always have a field "isExistant" every time that it succeeds at becoming json. 如果您确定每次成功成为json时,它将始终有一个字段“ isExistant”。

It doesn't make sense to expose a variable to the outside of an if let statement: 将变量暴露给if let语句的外部是没有意义的:


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 . 您可以将需要json的其余代码放在if You said you didn't know if nested if statements are "clever or even possible." 你说你不知道如果嵌套if语句是“聪明,甚至有可能。” 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 ! 如果您知道 JSON永远不应该为nil ,则可以使用! :

let json = ...!

You can make the variable global using a guard statement. 您可以使用guard语句使变量成为全局变量。 The code inside of the guard will only run if json is nil . 仅当json nilguard程序中的代码才会运行。 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: guard声明的主体必须退出封闭范围,例如,引发错误,从函数返回或带有标记的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.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM