如何从字符串值 Swift 中删除 Optional [英] How to remove Optional from String Value Swift

查看:87
本文介绍了如何从字符串值 Swift 中删除 Optional的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用没有可选扩展名的字符串值.我使用以下代码从 firebase 解析此数据:

I'd like to use a String value without the optional extension. I parse this data from firebase using the following code:

Database.database().reference(withPath:
    "Locations").child("Cities").observe(.value, with: { (snapShot) in
        if snapShot.exists() {
            let array:NSArray = snapShot.children.allObjects as NSArray

            for child in array {
                let snap = child as! DataSnapshot

                let cityName = snap.key
                let cityNameString = "(cityName)"
                if snap.value is NSDictionary {
                    let data:NSDictionary = snap.value as! NSDictionary
                    let lat = data.value(forKey: "lat")
                    let lng = data.value(forKey: "lng")
                    let radius = data.value(forKey: "radius")
                    let latstring = "(lat)"
                    let lngstring = "(lng)" 
                    let radiusstring = "(radius)" 

                    let city = CityObject(name: cityNameString , lat: latstring , lng: lngstring, radius: radiusstring)
                    print("Value is", laststring)  
                    self.selectCity(cityObject: city)
                }                            

            }
        }
    })

解析这些数据后,我尝试打印例如latstring 并获得以下输出:

after parsing this data i try to print e.g. the latstring and get following outpup:

值是可选的(52.523553)

Value is Optional(52.523553)

我的 CityObject 如下所示:

my CityObject looks like the following:

class CityObject{
var name: String?
var lat: String?
var lng: String?
var radius: String?

init(name: String?, lat: String?, lng: String?, radius: String?){
    self.name = name
    self.lat = lat
    self.lng = lng
    self.radius = radius
}

推荐答案

正如@GioR 所说,值为 Optional(52.523553) 因为 latstring 的类型是隐式的:String?.这是因为让 lat = data.value(forKey: "lat")会返回一个字符串吗?它隐式地设置了 lat 的类型.请参阅 https://developer.apple.com/documentation/objectivec/nsobject/1412591-值有关 value(forKey:) 的文档

Just like @GioR said, the value is Optional(52.523553) because the type of latstring is implicitly: String?. This is due to the fact that let lat = data.value(forKey: "lat") will return a String? which implicitly sets the type for lat. see https://developer.apple.com/documentation/objectivec/nsobject/1412591-value for the documentation on value(forKey:)

Swift 有多种处理 nil 的方法.可以帮助你的三个是,零合并运算符:

Swift has a number of ways of handling nil. The three that may help you are, The nil coalescing operator:

??

如果可选项为 nil,则此运算符会给出默认值:

This operator gives a default value if the optional turns out to be nil:

let lat: String = data.value(forKey: "lat") ?? "the lat in the dictionary was nil!"

守卫声明

guard let lat: String = data.value(forKey: "lat") as? String else {
    //Oops, didn't get a string, leave the function!
}

guard 语句可以让你把一个可选的变成非可选的等价物,或者你可以在它恰好为 nil 时退出函数

the guard statement lets you turn an optional into it's non-optional equivalent, or you can exit the function if it happens to be nil

如果让

if let lat: String = data.value(forKey: "lat") as? String {
    //Do something with the non-optional lat
}
//Carry on with the rest of the function

希望对你有帮助^^

这篇关于如何从字符串值 Swift 中删除 Optional的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆