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

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

问题描述

我想使用不带可选扩展名的String值.我使用以下代码从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)
                }                            

            }
        }
    })

解析此数据后,我尝试打印例如并获得以下结果:

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的方法.可能对您有帮助的三个方面, 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

希望这对您有帮助^^

Hope this helps ^^

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

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