无法摆脱Optional()字符串
我正在尝试更新服务器上的用户位置
使用此function
func updateloc(lat : String?, long : String?) { /code... let data = "lat=\(lat!)&long=\(long!)" }
这是代表
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { updateloc(String(manager.location?.coordinate.latitude), long: String(manager.location?.coordinate.longitude)) }
我有lat
和long
变量的可选(“”),无法摆脱它。
知道怎么做吗?
如果您打开像这样的纬度和经度值……
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { if let lat = manager.location?.coordinate.latitude, let long = manager.location?.coordinate.longitude { updateloc(String(lat), long: String(long)) } }
…然后你可以在updateloc
函数中完全避免选项:
func updateloc(lat : String, long : String) { // code... let data = "lat=\(lat)&long=\(long)" }
originalUser2
是对的…你应该养成安全展开选项的习惯,除非你有一个非常好的理由强行解开它们。 并且只是试图摆脱可选的代码将编译不是一个很好的理由。
updateloc(String(manager.location?.coordinate.latitude), ...)
该代码采用可选值( manager.location?.coordinate.latitude
),然后将其强制为String。 这导致字符串"Optional(42.0)"
或其他。 然后它将它传递给updateloc
。
我认为latitude
是双倍或类似的。 在转换为String之前,您需要处理可选的Double,如下所示:
func coordToString(val: Double?) -> String { return (val == nil ? "---" : String(val!)) } ... updateloc(coordToString(manager.location?.coordinate.latitude), ...)
我经常将类似于coordToString
函数作为模型对象本身(您的manager
或location
)的扩展。 我将它们命名为formattedLatitude
或类似的东西。 这样,您应用中的所有屏幕都将使用相同的格式设置该值,因为它们都将使用相同的代码来执行此操作。 这保留了规则,如果缺少纬度,在一个地方一起显示“—”。
为什么不检查位置是否nil
if let currentLocation = manager.location { updateloc(String(currentLocation.coordinate.latitude), long: String(currentLocation.coordinate.longitude)) }
并使用非可选参数声明updateloc()
func updateloc(lat : String, long : String) {...}
那么为何不
func updateloc(CLLocationCoordinate2D : coordinate) { let data = "lat=\(coordinate.latitude)&long=\(coordinate.longitude)" }
如果你还在使用String Interpolation ……
我认为您通过在查询字符串或请求正文中使用纬度和经度值发出HTTP请求来更新用户的位置。
您可以尝试以下几种方法:
-
Nil coalescing operator
如果a包含值,则nil合并运算符(a ?? b)展开可选a;如果a为n,则返回默认值b 。 表达式a始终是可选类型。 表达式b必须与存储在a中的类型匹配。
假设我们在请求中发送任何不存在的值的空字符串:
func updateloc(lat : Double?, long : Double?) { let latitude = String(lat) ?? "" let longitude = String(long) ?? "" let data = "lat=\(latitude)&long=\(longitude)" ... }
-
Optional binding
您使用可选绑定来确定可选项是否包含值,如果是,则使该值可用作临时常量或变量。
假设我们没有触发任何不存在的值的请求:
func updateloc(lat : Double?, long : Double?) { if let latitude = lat, longitude = long { let data = "lat=\(latitude)&long=\(longitude)" } else { // Don't make a request.. } }
顺便说一下,将位置作为CLLocationCoordinate2D
类型传递更可读,而不是分别传递纬度和经度:
func updateloc(coordinate: CLLocationCoordinate2D?) { if let coordinate = coordinate { let data = "lat=\(coordinate.latitude)&long=\(coordinate.longitude)" } else { // Don't make a request.. } }
这不是这个问题的确切答案,而是这类问题的一个原因。
就我而言,我无法使用“if let”和“guard let”从String中删除Optional。
我是Any的情况
let dict1 : [String:Any] = ["key":("value" as String?) as Any] //use guard let, if let, default value but the string is still optional if let a = dict1["key"] { print("With Any \(a)") } //Output is: With Any Optional("value")
如果是AnyObject
let dict : [String:AnyObject] = ["key":("value" as String?) as AnyObject] if let a = dict["key"] { print("With AnyObject \(a)") } //Output is: With AnyObject value
因此,使用AnyObject而不是Any来从swift中的字符串中删除可选项。