2016-04-28 108 views
0

在我的「共享位置」功能中,我希望用戶能夠發送以長/緯度座標形式共享其當前位置的文本。我用我實現的錯誤是「上下文類型」字符串「不能用於數組文字」。如何或者什麼是正確的代碼來實現?如何通過短信文本發送用戶的經度和緯度座標

這裏是我的代碼

@IBAction func shareLocation(sender: AnyObject) { 
    // Send user coordinates through text 

    if !MFMessageComposeViewController.canSendText() { 
     print("SMS services are not available") 

     var coordinate: CLLocationCoordinate2D 

     messageVC!.body = [coordinate.latitude, coordinate.longitude]; 
     messageVC!.recipients = [LookoutCell.description()]; 
     messageVC!.messageComposeDelegate = self; 


     self.presentViewController(messageVC!, animated: false, completion: nil) 
    } 
} 

回答

1

有很多的錯誤與此代碼:

  1. 你檢查,如果你不能通過!MFMessageComposeViewController.canSendText()發送文本,如果你可以」然後發送t(括號內需要提前結束)
  2. 你聲明var coordinate: CLLocationCoordinate2D沒有值,不會編譯
  3. 經緯度是雙倍的,所以你需要字符串格式化來輸出這些值
  4. body是一個字符串,您正在嘗試向它發送一個數組。
  5. 試試這個:messageVC!.body = String(format: "Lat: %.4f, Long: %.4f", coordinate.latitude, coordinate.longitude) - 你需要考慮格式化引導更多的細節(你可以開始here
+0

哇,我不知道這麼多是不正確的。我還會檢查格式指南以獲取更多幫助。 – jonB22