使 if 语句的变量成为全局变量
Making a variable from if statement global
在编码 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
}
这可能吗?如果不是,我可以在这个里面做一个 if
声明,但我不认为那会很聪明,甚至不可能。
delclare jsonIsExistant 在你想要的地方。如果您正在制作一个 iOS 应用程序,则比上面 viewDidLoad()
创建变量
var jsonIsExistant: String?
然后在这个时候使用它
do {
if let json = try JSONSerialization.jsonObject(with: data) as? [String: String],
let tempJsonIsExistant = json["isExistant"] {
jsonIsExistant = tempJsonIsExistant
}
}
虽然可以这样重写
do {
if let json = try JSONSerialization.jsonObject(with: data) as? [String: String] {
jsonIsExistant = json["isExistant"]
}
} catch {
//handle error
}
如果用第二种方式处理,那么你必须在使用前检查 jsonIsExistant 是否为 nil,或者你可以立即用 !如果您确定它每次成功成为 json.
时总会有一个字段 "isExistant"
将变量暴露在 if let
语句之外没有意义:
if let json = ... {
//This code will only run if json is non-nil.
//That means json is <i>guaranteed</i> to be non-nil here.
}
//This code will run whether or not json is nil.
//There is <b>not</b> a guarantee json is non-nil.
您还有其他一些选择,具体取决于您想做什么:
您可以将需要 json
的其余代码放在 if
中。你说你不知道嵌套的 if
语句是否 "clever or even possible." 它们是可能的,并且程序员经常使用它们。您也可以将其提取到另一个函数中:
func doStuff(json: String) {
//do stuff with json
}
//...
if let json = ... {
doStuff(json: json)
}
如果你知道 JSON 不应该是 nil
,你可以用 !
强制解包:
let json = ...!
您可以使用 guard
语句使变量成为全局变量。如果 json
是 nil
,guard
中的代码只会 运行。 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.
}
在编码 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
}
这可能吗?如果不是,我可以在这个里面做一个 if
声明,但我不认为那会很聪明,甚至不可能。
delclare jsonIsExistant 在你想要的地方。如果您正在制作一个 iOS 应用程序,则比上面 viewDidLoad()
创建变量
var jsonIsExistant: String?
然后在这个时候使用它
do {
if let json = try JSONSerialization.jsonObject(with: data) as? [String: String],
let tempJsonIsExistant = json["isExistant"] {
jsonIsExistant = tempJsonIsExistant
}
}
虽然可以这样重写
do {
if let json = try JSONSerialization.jsonObject(with: data) as? [String: String] {
jsonIsExistant = json["isExistant"]
}
} catch {
//handle error
}
如果用第二种方式处理,那么你必须在使用前检查 jsonIsExistant 是否为 nil,或者你可以立即用 !如果您确定它每次成功成为 json.
时总会有一个字段 "isExistant"将变量暴露在 if let
语句之外没有意义:
if let json = ... {
//This code will only run if json is non-nil.
//That means json is <i>guaranteed</i> to be non-nil here.
}
//This code will run whether or not json is nil.
//There is <b>not</b> a guarantee json is non-nil.
您还有其他一些选择,具体取决于您想做什么:
您可以将需要 json
的其余代码放在 if
中。你说你不知道嵌套的 if
语句是否 "clever or even possible." 它们是可能的,并且程序员经常使用它们。您也可以将其提取到另一个函数中:
func doStuff(json: String) {
//do stuff with json
}
//...
if let json = ... {
doStuff(json: json)
}
如果你知道 JSON 不应该是 nil
,你可以用 !
强制解包:
let json = ...!
您可以使用 guard
语句使变量成为全局变量。如果 json
是 nil
,guard
中的代码只会 运行。 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.
}