2016-03-15 30 views
0

i集成谷歌廣場API來獲取用戶的位置投類型的值「NSTaggedPointerString」到」的NSArray,我要取的‘區域’,‘國家’,‘城市’,而我試圖獲得價值我的應用程序崩潰。請發表烏爾答案無法在谷歌將阿比

這裏我的示例代碼

func mapView(mapView: GMSMapView, idleAtCameraPosition position: GMSCameraPosition) 
{ 
    dispatch_async(dispatch_get_main_queue(), {() -> Void in 
     let centerLocation = CLLocation(latitude: position.target.latitude, longitude: position.target.longitude) 
     CLGeocoder().reverseGeocodeLocation(centerLocation, completionHandler: 
      {(placemarks, error) in 
      if (error == nil && placemarks!.count>0) 
      { 

       let placemark = CLPlacemark(placemark: placemarksArray[0] as! CLPlacemark) 

       let latitude = String(format: "%.8f",position.target.latitude) 
       let longitude = String(format: "%.8f",position.target.longitude) 


       if let addrList = placemark.addressDictionary 
       { 

        print("address==\(address)") 
        let addStr = address?["FormattedAddressLines"] as! [String] 

        let addStr1 = address?["City"] as! [String] 
        let addStr2 = address?["State"] as! [String] 
        let addStr3 = address?["SubLocality"] as! [String] 

        NSLog("%@\n%@\n%@", addStr1,addStr2,addStr3) 
        self.addressLabel.text = addStr.joinWithSeparator(",") 



       } 
      } 
     }) 
    }) 
} 

我的輸出:

address=={ 
City = "New Delhi"; 
Country = India; 
CountryCode = IN; 
FormattedAddressLines =  (
    "Mayur Vihar", 
    "New Delhi", 
    "Delhi 110092", 
    India 
); 
Name = 110092; 
State = Delhi; 
SubAdministrativeArea = Delhi; 
SubLocality = "Mayur Vihar"; 
ZIP = 110092; 

}

和我的崩潰報告:

Could not cast value of type 'NSTaggedPointerString' (0x1064cb860) to 'NSArray' (0x1064cb900). 
+0

哪條線該應用是否會崩潰嗎? messGe很清楚;您是鑄造一個字符串數組,所以成才,你認爲是一個數組實際上是一個字符串 - 當然,城市。國家和sublocality不是數組但你是壓低鑄造他們作爲陣列 – Paulw11

+0

應用在addStr1,addStr2,addStr3 – user3458924

+0

得到崩潰是因爲他們是字符串,而不是字符串數組,你是迫使他們向下轉換到 – Paulw11

回答

0

城市和國家有字符串值和FormattedAddressLines有Arry。

使用這樣

let addStr1 = address["City"] as! String 
let addStr2 = address["State"] as! String 
let FormattedAddressLines = address["FormattedAddressLines"] as![String] 
+0

你能提供一點上,爲什麼你的答案工作環境的:-) – MLavoie