变量从未发生过变化……或者在初始化之前使用了常量
Variable was never mutated...or Constant being used before initialized
所以我想取回 json膳食数据并在此功能之外使用它。
但是,似乎无论我将 json 变量放在哪里,我都会收到错误消息。将其更改为 let 也可以,尽管是不同的。任何见解将不胜感激!
错误:
Constant 'json' used before being initialized // Variable 'json' was never mutated; consider changing to 'let' constant
func getApiDetailData(completed: @escaping () -> ()) {
var json: Any?
let urlString = "https://www.themealdb.com/api/json/v1/1/lookup.php?i=\(id)"
let url = URL(string: urlString)
URLSession.shared.dataTask(with: url!) { (data, response, error) in
do {
let json = try JSONSerialization.jsonObject(with: data!)
print("\(json)Testing")
DispatchQueue.main.async {
completed()
}
}
catch {
print("Error getting detail JSON data:\(error)")
}
guard let json = json as? [String : Any],
let jsonMeals = json["meals"] as? [String: Any] else {
print("No meals in json \(error?.localizedDescription)")
return
}
print("testing jsonMeals\(jsonMeals)")
}.resume()
}
尝试类似此示例代码的操作:
func getApiDetailData(completed: @escaping () -> ()) {
// var json: Any? // <-- remove, never used
let urlString = "https://www.themealdb.com/api/json/v1/1/lookup.php?i=\(id)"
let url = URL(string: urlString)
URLSession.shared.dataTask(with: url!) { (data, response, error) in
do {
let jsonData = try JSONSerialization.jsonObject(with: data!)
print("\(jsonData) Testing")
guard let json = jsonData as? [String : Any],
let jsonMeals = json["meals"] as? [[String: Any]] else {
print("No meals in json \(error?.localizedDescription)")
completed() // <-- here
return
}
print("testing jsonMeals \(jsonMeals)")
completed() // <-- here
}
catch {
print("Error getting detail JSON data:\(error)")
completed() // <-- here
}
}.resume()
}
或者,如果您想要 return jsonMeals
结果:
func getApiDetailData(completed: @escaping ([[String: Any]]?) -> ()) { // <-- here
// var json: Any? // <-- remove, never used
let urlString = "https://www.themealdb.com/api/json/v1/1/lookup.php?i=\(id)"
let url = URL(string: urlString)
URLSession.shared.dataTask(with: url!) { (data, response, error) in
do {
let jsonData = try JSONSerialization.jsonObject(with: data!)
print("\(jsonData) Testing")
guard let json = jsonData as? [String : Any],
let jsonMeals = json["meals"] as? [[String: Any]] else {
print("No meals in json \(error?.localizedDescription)")
completed(nil) // <-- here
return
}
print("testing jsonMeals \(jsonMeals)")
completed(jsonMeals) // <-- here
}
catch {
print("Error getting detail JSON data:\(error)")
completed(nil) // <-- here
}
}.resume()
}
所以我想取回 json膳食数据并在此功能之外使用它。 但是,似乎无论我将 json 变量放在哪里,我都会收到错误消息。将其更改为 let 也可以,尽管是不同的。任何见解将不胜感激!
错误:
Constant 'json' used before being initialized // Variable 'json' was never mutated; consider changing to 'let' constant
func getApiDetailData(completed: @escaping () -> ()) {
var json: Any?
let urlString = "https://www.themealdb.com/api/json/v1/1/lookup.php?i=\(id)"
let url = URL(string: urlString)
URLSession.shared.dataTask(with: url!) { (data, response, error) in
do {
let json = try JSONSerialization.jsonObject(with: data!)
print("\(json)Testing")
DispatchQueue.main.async {
completed()
}
}
catch {
print("Error getting detail JSON data:\(error)")
}
guard let json = json as? [String : Any],
let jsonMeals = json["meals"] as? [String: Any] else {
print("No meals in json \(error?.localizedDescription)")
return
}
print("testing jsonMeals\(jsonMeals)")
}.resume()
}
尝试类似此示例代码的操作:
func getApiDetailData(completed: @escaping () -> ()) {
// var json: Any? // <-- remove, never used
let urlString = "https://www.themealdb.com/api/json/v1/1/lookup.php?i=\(id)"
let url = URL(string: urlString)
URLSession.shared.dataTask(with: url!) { (data, response, error) in
do {
let jsonData = try JSONSerialization.jsonObject(with: data!)
print("\(jsonData) Testing")
guard let json = jsonData as? [String : Any],
let jsonMeals = json["meals"] as? [[String: Any]] else {
print("No meals in json \(error?.localizedDescription)")
completed() // <-- here
return
}
print("testing jsonMeals \(jsonMeals)")
completed() // <-- here
}
catch {
print("Error getting detail JSON data:\(error)")
completed() // <-- here
}
}.resume()
}
或者,如果您想要 return jsonMeals
结果:
func getApiDetailData(completed: @escaping ([[String: Any]]?) -> ()) { // <-- here
// var json: Any? // <-- remove, never used
let urlString = "https://www.themealdb.com/api/json/v1/1/lookup.php?i=\(id)"
let url = URL(string: urlString)
URLSession.shared.dataTask(with: url!) { (data, response, error) in
do {
let jsonData = try JSONSerialization.jsonObject(with: data!)
print("\(jsonData) Testing")
guard let json = jsonData as? [String : Any],
let jsonMeals = json["meals"] as? [[String: Any]] else {
print("No meals in json \(error?.localizedDescription)")
completed(nil) // <-- here
return
}
print("testing jsonMeals \(jsonMeals)")
completed(jsonMeals) // <-- here
}
catch {
print("Error getting detail JSON data:\(error)")
completed(nil) // <-- here
}
}.resume()
}