0

Hi there I made this code as example :

struct myData{
var payments: Int!
var paymentDate : String!
var itemNumber : String!
              }

var myDataArray = [myData(payments:100, paymentDate: "10/10/2010", itemNumber: "1001"),
               myData(payments:200, paymentDate: "10/10/2010", itemNumber: "1002" ),
               myData(payments:500, paymentDate: "10/10/2010", itemNumber: "2001" ),
               myData(payments:400, paymentDate: "10/10/2010", itemNumber: "3210" ),
               myData(payments:150, paymentDate: "10/10/2010", itemNumber: "1234" ),]

What I'm asking for how could I get the results for a specific object for that structure Data. for Example If I want to print all the payments result or specific IndexPath

really I tried hard and I searched hard but without any clear answers

1
  • Yo print a specific you do: myDataArrat[0] and you will print your first value. I you want to print all of them, you can do a foreach loop. Commented Feb 1, 2017 at 16:53

2 Answers 2

2

If you only want to print the data, see PEEJWEEJ's answer. If you want an array of, say, all payments properties of the individual array entries, use

myDataArray.map { $0.payments }

which essentially means 'go through each element of myDataArray, execute element.payments on each of these elements, then build an array with the results'.

By the way, it seems like your properties are always available, i.e. never nil. In that case you don't need to add a ! at the end of the declaration, so

struct MyData {
  let payments: Int
  let paymentDate: String
  let itemNumber: String
}

would be sufficient.

Sign up to request clarification or add additional context in comments.

Comments

1

print(myDataArray[0].payments) would print the first item's payments. (so 100)

If you want to print all of them, you could loop through it like so: myDataArray.forEach { print($0.payments) }

1 Comment

Thanks for you help dude

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.