在我的“共享位置”功能中,我希望用户能够发送以长/纬度坐标形式共享当前位置的文本。我所实现的错误是“上下文类型“字符串”不能与数组文字一起使用”。如何或什么是正确的代码来实现?
这是我的代码
@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)
}
}
最佳答案
这段代码有很多错误:
- 您正在检查是否无法通过
!MFMessageComposeViewController.canSendText()
发送短信,如果不能的话就发送(括号需要提前结束) - 您声明
var coordinate: CLLocationCoordinate2D
没有值(value),无法编译 - 纬度和经度是 double ,因此您需要字符串格式来输出这些值
-
body
是一个字符串,您试图向它发送一个数组。 - 试试这个:
messageVC!.body = String(format: "Lat: %.4f, Long: %.4f", coordinate.latitude, coordinate.longitude)
- 您需要查看格式指南以获取更多详细信息(您可以从 here 开始)
关于xcode - 如何通过短信发送用户经纬度坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36905203/