R
R
rufatz12017-10-24 08:10:49
iOS
rufatz1, 2017-10-24 08:10:49

How to select data from one array based on data from another?

Hello
Such task:
There is an array in which Int-s are stored, [0,2,3] for example.
There is a second array that stores objects of the Currency class [bc.Currency, bc.Currency, ...].
How can you derive an object from Currency based on an index that is equal to the value from an Int array?
For example: To display only objects whose index is 0, 2, 3.
Thank you in advance for your help!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
rufatz1, 2017-10-24
@rufatz1

Solved the problem
By creating two constants
let defaultData - passed an array containing Int indices
here let currencyData - passed an array containing class objects here
Then unpacked defaultData through for in and set it as an index value in currencyData.

func svodka () {
        let defaultData = UserDefaults.standard.object(forKey: "Saved Default Selection") as? [Int]
        let currencyData = currenciesArray
        for item in defaultData! {
            print (currencyData[item].codeOfCurrency)

A
Alexey, 2017-10-24
@qsunnx

defaultData!.map{elem in print(currencyData[elem].codeOfCurrency) }
Maybe

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question