Srujan Simha Adicharla
Srujan Simha Adicharla

Reputation: 3723

Swift: Adding array values to in-app email message body

I have some values in an array and want to add them to the in-app email message body. I tried running a for loop and adding all the array values to the message body but unfortunately only the last value in array is getting displayed. Something like this..

for(var i=0; i < userDataName.count; i++)
    {
        mc.setMessageBody("\(userDataName[i]) - \(userDataStatus[i])", isHTML: false)
    }

I know it's dumb but couldn't find any better way.

Upvotes: 0

Views: 516

Answers (1)

Antonio
Antonio

Reputation: 72760

I don't think that setMessageBody appends (nor its name indicates that), so I think you should construct the body first, using a string variable, and then set the body at the end of the loop:

var body = ""
for(var i=0; i < userDataName.count; i++)
{
    body += "\(userDataName[i]) - \(userDataStatus[i])\n"
}
mc.setMessageBody(body, isHTML: false)

or even:

var body = (0..<userDataName.count)
    .map { index in "\(userDataName[index]) - \(userDataStatus[index])" }
    .reduce("") { $0 + $1 + "\n"}

mc.setMessageBody(body, isHTML: false)

Upvotes: 3

Related Questions