2015-11-02 42 views
3

所以我是新來的迅速,我正在試驗谷歌地圖。 我不知道爲什麼這條線的工作原理:爲什麼我得到這個錯誤:結果值在'? :'表達式有不匹配的類型'[String]?'和'String'

let state = p?.administrativeArea!= nil? 2 P .administrativeArea: 「nil6」

但不是這一行:

讓areaOfInterest = P .areasOfInterest =零?!? p .areasOfInterest: 「nil7」

我收到此錯誤信息的areaOfInterest行:

在「結果值? :'表達式有不匹配的類型'[String]?'和'字符串'

在此先感謝。

CLGeocoder().reverseGeocodeLocation(userLocation) { (placemarks, error) -> Void in 

     if error != nil 
     { 
      print(error) 
     } 
     else 
     { 

      let p = placemarks?[0] 

      print(p) 
      let subThoroughfare = p?.subThoroughfare != nil ? p?.subThoroughfare : "nil1" 
      let thoroughfare = p?.thoroughfare != nil ? p?.thoroughfare : "nil2" 
      let country = p?.country != nil ? p?.country : "nil3" 
      let postal = p?.postalCode != nil ? p?.postalCode : "nil4" 
      let city = p?.locality != nil ? p?.locality : "nil5" 
      let state = p?.administrativeArea != nil ? p?.administrativeArea : "nil6" 
      let areaOfInterest = p?.areasOfInterest != nil ? p?.areasOfInterest : "nil7" 

      self.addressLabel.text = "\(subThoroughfare!) \(thoroughfare!) \n \(city!), \(state!) \n \(country!) \(postal!)" 
     } 

    } 
} 

回答

3

這是因爲所有你正在訪問一個標的其他屬性都是字符串,除了感興趣的區域,這是刺的(可選)陣列。當它沒有辦法工作時,你正試圖施放它。

如果你真的要格式化你的錯誤那樣,你可以使用:

let areaOfInterest = p?.areasOfInterest != nil ? p?.areasOfInterest : ["nil7"]

+0

謝謝你這麼多你的幫助和解釋。 –

0

試圖把[「nil7」],而不是「nil7」

+0

非常感謝您的幫助 –

相關問題