2016-04-05 88 views
1

我试图更新服务器无法摆脱可选()字符串

用户位置使用此功能

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)) 

    } 

我有可选( “”)为latlong变量,无法摆脱它。

任何想法如何做到这一点?

+3

不要强制拆包。永远。你应该学会如何[正确处理optionals。](http://stackoverflow.com/a/36360605/2976878) – Hamish

+0

@ originaluser2“Ever”?这绝对没有帮助。这是语言的原因,在很多情况下你都需要它。 –

+2

@EwanMellor好吧,也许有一对*边缘情况下的力量解包是好的...但是,当开始与可选项,避免像瘟疫一样的力量解开是要走的路。 – Hamish

回答

4

如果你解开这样的经度和纬度值...

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是正确的..你应该习惯于安全地解开可选项,除非你有强有力的理由解开它们。只是试图摆脱可选的代码将编译不是一个很好的理由。

3

updateloc(String(manager.location?.coordinate.latitude), ...)

即代码正在一个可选值(manager.location?.coordinate.latitude),然后迫使为字符串。这导致字符串"Optional(42.0)"或其他。然后它传递给updateloc

我推测latitude是Double或类似的。你需要转换前处理可选双为字符串,像这样的东西:

func coordToString(val: Double?) -> String { 
    return (val == nil ? "---" : String(val!)) 
} 

... 

updateloc(coordToString(manager.location?.coordinate.latitude), ...) 

我经常把类似coordToString功能的扩展模型对象本身(您managerlocation)上。我会把它们命名为formattedLatitude或类似的东西。这样,应用中的所有屏幕都将使用相同的格式来表示该值,因为它们都将使用相同的代码来执行此操作。这保留了“如果纬度缺失,在一个地方显示”---“'的规则。

+0

return val?.description ?? “---” – user3441734

3

如果该位置不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)" 
} 

如果你使用为什么不检查字符串插值无论如何...

2

我想你正在更新用户的位置,通过请求中的查询字符串或正文中的纬度和经度值进行HTTP请求。

有几个选项,你可以尝试:

  1. Nil coalescing operator

的零合并运算(A?B)解开可选一个如果包含一个值,或返回默认值b if a。表达式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
  • 您使用可选绑定来确定可选是否包含值,如果是,则创建该值lue可用作临时常量或变量。

    假设我们不触发任何不存在的值的请求:

    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.. 
        } 
    }