[英]if no value return in set double to a set number with a guard let statement
在其下面的代碼中,從未使用過else語句后發出警告,表示經度和緯度,如果我的字典中不包含經度和緯度值,則會將它們設置為let經度= Double(151.209900)和let latitude = Double (-33.865143)? 或者我在警衛隊讓陳述中做錯了什么
guard let dictionary = snapshot.value as? [String : AnyObject] else { return }
guard let latitude = dictionary["Latitude"] as? String else {
let latitude = Double(-33.865143)
return
}
guard let longitude = dictionary["Longitude"] as? String else {
let longitude = Double(151.209900)
return
}
guard let latDouble = Double(latitude) else { return }
guard let longDouble = Double(longitude) else { return }
為緯度和經度設置的值將永遠不會使用,因為它們的范圍僅限於guard
我可能會這樣做:
guard let dictionary = snapshot.value as? [String : AnyObject] else { return }
let latitude = Double(dictionary["Latitude"] as? String ?? "-33.865143")
let longitude = Double(dictionary["Longitude"] as? String ?? "151.209900")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.