简体   繁体   English

在Swift中将双打数组附加到邮件

[英]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: 所以这是我的想法:

  • Option 1: Convert the array of doubles to a string and send it as a plain/text file. 选项1:将双精度数组转换为字符串,然后将其作为纯文本文件发送。 My intuition, however, tells me that this is not a nice solution. 但是我的直觉告诉我,这不是一个好的解决方案。 I am not a fan of parsing. 我不喜欢解析。
  • Option 2: Encode the array somehow and send it using another mimetype other than plain/text. 选项2:以某种方式对数组进行编码,并使用除纯文本/文本之外的其他mimetype发送数组。 I explored some options in IANA mime Types , but I am not familiar at all and do not know where to start with. 我探索了IANA mime Types中的一些选项,但我一点都不陌生,也不知道从哪里开始。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM