在 Swift 中以循环作为正文发送电子邮件
Sending an email with a loop as body in Swift
我想发送一封包含不同步骤和值的电子邮件,具体取决于用户在图像上编辑的内容。为此,我使用 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(["blablabla@gmail.com"])
mail.setSubject("Filtro \(filtro)")
mail.setMessageBody(body, isHTML: true)
present(mail, animated: true)
} else {
print("Error presenting email app.")
}
}
所以打印工作如我所料,但问题是将这些打印件放入 sendMail 函数的主体中...
我试过了:
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"))
但是只有最后一步被写入正文的电子邮件而不是数组...
拜托,谁能帮帮我?
提前致谢!
你可以试试下面的代码
var steps: String = ""
for n in 1...self.i {
steps += "Step \(n): \(self.filterUserDefaults.string(forKey: "Step_\(n)")!)"
}
你快完成了,你需要初始化数组变量并使用 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"))
我想发送一封包含不同步骤和值的电子邮件,具体取决于用户在图像上编辑的内容。为此,我使用 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(["blablabla@gmail.com"])
mail.setSubject("Filtro \(filtro)")
mail.setMessageBody(body, isHTML: true)
present(mail, animated: true)
} else {
print("Error presenting email app.")
}
}
所以打印工作如我所料,但问题是将这些打印件放入 sendMail 函数的主体中...
我试过了:
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"))
但是只有最后一步被写入正文的电子邮件而不是数组...
拜托,谁能帮帮我?
提前致谢!
你可以试试下面的代码
var steps: String = ""
for n in 1...self.i {
steps += "Step \(n): \(self.filterUserDefaults.string(forKey: "Step_\(n)")!)"
}
你快完成了,你需要初始化数组变量并使用 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"))