0

Explanation

In my app I want to add the ability to export the data of a member. Im talking about all the transactions of the member. This is what I have, I hope it's clear what I'm asking. What do I have to edit, that it works? Or is my way the right?

var str: [String] = []
for trans in (member?.transactions!)! {
    let trnsctnr = trans.reason
    let trnsctna = String(trans.money)
    var trnsctnd = ""
    if let trnsd = trans.date{
        trnsctnd = dateFormatter.string(from: trnsd)
    }
    str = [trnsctnr, trnsctna, trnsctnd] as! [String]
}

This code doesn't work. I want to add this in a 'MFMailComposerController' and than this error is shown :

Cannot convert value of type 'Never' to expected argument type 'String'

Core Data Model

So I want to export the data of all transactions which the member did.

What I did as a computed property:

struct stringForMFMail {
            let trnsctnr = transaction?.reason
            var trnsctna = ""
            if let trns  = transaction?.money {
                trnsctna = String(trns)
                
            }
            var trnsctnd = ""
            if let trnsd = transaction?.date{
                trnsctnd = dateFormatter.string(from: trnsd)
            }
            let dataString = [trnsctnr, trnsctna, trnsctnd]
            

            }

This doesn't work at all, so I'm sure I did it completely wrong. There are some errors like: Struct declaration cannot close over value 'self' defined in outer scope. Totally there are 12 error in this little part so I did something really wrong.

12
  • 2
    What do you mean by "export", and why exactly do you need to re-express the data as a string? The data is already the data; what is the point of "exporting" it? Commented Aug 13, 2018 at 15:42
  • With exporting I mean, that I want to add this to a MFMailComposerController which I already have, so thats not important. What do you mean with re-express the data as a string? Commented Aug 13, 2018 at 15:53
  • 1
    Well, I think you've answered both questions. You really do want to re-express it as a string because you want the user to be able to mail a statement of some kind. That's fine. So what's the question exactly? Is the code that you showed not working somehow? "I hope it's clear what I'm asking." It isn't. "What do I have to edit, that it works?" Explain how it doesn't "work". What do you get and how does that differ from what you desire? Commented Aug 13, 2018 at 15:55
  • 2
    That's great. But do you notice anything? You didn't say that anywhere in your question! If that's what the question is supposed to be about, edit the question so that it is what it's about! Show how you "add it to the message body of the mail composer". You say "that is not important" but obviously it is very important because that's where the problem is. We cannot guess what the question is; you have to tell us. Tell us the error. Show us the code where the error occurs and tell us which line it occurs on. That is how to ask a question on Stack Overflow. Commented Aug 13, 2018 at 16:00
  • 1
    Please read the section Computed Properties in Swift Language Guide - Properties Commented Aug 13, 2018 at 16:08

1 Answer 1

2

In the class Transaction add this computed property, it returns the three values – if present – comma separated.

var stringForMFMail : String {
     var result = self.reason ?? "" 
     result += "\(self.money)"

     if let trnsd = self.date {
        result += ", \(dateFormatter.string(from: trnsd))"
     }
     return result
}

Add also this date formatter in Transaction

let dateFormatter : DateFormatter = {
    let formatter = DateFormatter()
    formatter.locale = Locale(identifier: "en_US_POSIX")
    formatter.dateFormat = "dd:MM:yyyy"
    return formatter
}()

Now you can populate the array

var mailStrings = [String]()
if let transactions = member?.transactions {
   mailStrings = member.transactions.map{ $0.stringForMFMail }
}

If you need one single string, join the array

let str = mailStrings.join(separator: ", ")
Sign up to request clarification or add additional context in comments.

12 Comments

Great, I really appreciate your work. I added the computed property to the class and got these errors: Initializer for conditional binding must have Optional type, not 'Double' , in the line if let tans = self.money and the error Use of unresolved identifier 'dateFormatter' in the line: result += ", (dateFormatter.string(from: trnsd))" I tried it with DateFormatter with a capital D and then got this error : Instance member 'string' cannot be used on type 'DateFormatter'; did you mean to use a value of this type instead?
I fixed the conditional binding error. You have to copy also the date formatter instance used in your code into the Transaction class
If there is always a reason write "\(self.reason!) or use optional bindings like for the date. I cannot help with the date because there is not enough information.
join(separator: "\n")
Backslash (\`) not slash (/`). Isn't it ⇧⌥7 on the German keyboard?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.