[英]Attach an array of doubles to a mail in Swift
I am trying to attach an array of doubles to a mail using the MFMailComposeViewController
class. 我正在尝试使用
MFMailComposeViewController
类将双精度数组附加到邮件。 So far this is my code in the ViewController
class: 到目前为止,这是我在
ViewController
类中的代码:
func prepareMail(data:[Double]) {
// Compose the mail
let mailComposer = MFMailComposeViewController()
mailComposer.mailComposeDelegate = self
mailComposer.setToRecipients(["mail@mail.com"])
mailComposer.setSubject("subject")
mailComposer.setMessageBody("Hello ", isHTML: false)
// Name data files (accelerometer + label)
let fileName = "file"
if let dataToAttach = data {
//Attach File
mailComposer.addAttachmentData(dataToAttach, mimeType: "text/plain", fileName: "\(fileName)")
self.present(mailComposer, animated: true, completion: nil)
}
}
}
This code raises the following message: 此代码引发以下消息:
initializer for conditional binding must have Optional type, not [Double]
条件绑定的初始值设定项必须具有Optional类型,而不是[Double]
So here are my thoughts: 所以这是我的想法:
mimetype
other than plain/text. mimetype
发送数组。 I explored some options in IANA mime Types , but I am not familiar at all and do not know where to start with. I am not sure how to proceed. 我不确定如何进行。
Your problem is this line if let dataToAttach = data
because your data is [Double]
and can't be nil
, so you don't need to check of is nil
, or you can change the parameters type to [Double]?
您的问题是
if let dataToAttach = data
这一行if let dataToAttach = data
因为您的数据为[Double]
并且不能为nil
,因此您无需检查is为nil
,或者可以将参数类型更改为[Double]?
to avoid this compiler error. 以避免此编译器错误。
Replacing this: 替换为:
if let dataToAttach = data {
//Attach File
mailComposer.addAttachmentData(dataToAttach, mimeType: "text/plain", fileName: "\(fileNames[i])")
self.present(mailComposer, animated: true, completion: nil)
}
by this: 这样:
func prepareMail(data:[Double]) {
// Compose the mail
let mailComposer = MFMailComposeViewController()
mailComposer.mailComposeDelegate = self
mailComposer.setToRecipients(["mail@mail.com"])
mailComposer.setSubject("subject")
mailComposer.setMessageBody("Hello ", isHTML: false)
// Name data files (accelerometer + label)
let fileName = "file"
if let dataToAttach = data.map({String($0)}).joined(separator: "\n").data(using: .utf8)
{
mailComposer.addAttachmentData(dataToAttach, mimeType: "text/plain", fileName: "\(fileNames[i])")
self.present(mailComposer, animated: true, completion: nil)
}
}
will be enough. 足够了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.