在Swift中发送以循环为主体的电子邮件。

问题描述 投票:0回答:2

我想根据用户在图片上编辑的内容,用不同的步骤和值发送电子邮件。为此,我使用UserDefaults来保存值。

然后,我把接下来的代码放到一个UIAlertController中。

alert.addAction(UIAlertAction(title: "Send", style: .default)
        { action -> Void in
            //i is the last step register

            let nombre = alert.textFields![0]

            for n in 1...self.i {
                print("Step \(n): \(self.filterUserDefaults.string(forKey: "Step_\(n)")!)")
            }

            let filters = [
                "Brillo",
                "Exposicion",
                "Contraste",
                "Saturacion",
                "Saturacion_color",
                "Temperatura",
                "Blanco_Negro",
                "HUE",
                "Tintado_Rojo",
                "Tintado_Rosa",
                "Tintado_Naranja",
                "Tintado_Amarillo",
                "Tintado_Purpura",
                "Tintado_Verde",
                "Tintado_Azul",
                "Tintado_Marron",
                "Tintado_Gris"]
            for filter in filters {
                print("\(filter) = \(self.filterUserDefaults.float(forKey: filter).roundTo(places: 3))")
            }
            self.sendMail(filtro: nombre.text!, body: "XXXX")
        })
        present(alert, animated: true)
    }
    func sendMail(filtro: String, body: String) {
        if MFMailComposeViewController.canSendMail() {
            let mail = MFMailComposeViewController()
            mail.mailComposeDelegate = self
            mail.setToRecipients(["[email protected]"])
            mail.setSubject("Filtro \(filtro)")
            mail.setMessageBody(body, isHTML: true)
            present(mail, animated: true)
        } else {
            print("Error presenting email app.")
        }
    }

所以打印的效果和我预期的一样 但问题是要把这些打印结果放到sendMail函数的主体中...

我试过了,但只有最后一步写进了UIAlertController中。

            var steps: [String]?

            for n in 1...self.i {
                steps = ["Step \(n): \(self.filterUserDefaults.string(forKey: "Step_\(n)")!)"]
            }
            .
            .
            .
            self.sendMail(filtro: nombre.text!, body: steps!.joined(separator: "\n"))

但只有最后一步写进了邮件正文,而不是数组... ...

请问,谁能帮帮我?

先谢谢你了!

arrays swift for-loop mfmailcomposeviewcontroller
2个回答
0
投票

你差不多了,你需要初始化你的数组变量,然后使用 append

var steps = [String]()

for n in 1...self.i {
    steps.append("Step \(n): \(self.filterUserDefaults.string(forKey: "Step_\(n)")!)")
}
...
self.sendMail(filtro: nombre.text!, body: steps.joined(separator: "\n"))

1
投票

你可以试试以下代码

var steps: String = ""
for n in 1...self.i {
steps += "Step \(n): \(self.filterUserDefaults.string(forKey: "Step_\(n)")!)"
}
© www.soinside.com 2019 - 2024. All rights reserved.