Search code examples
iosswiftcomputed-properties

Swift - Computed Property Return


I have array of dictionaries that I will use for cells in collectionView. I'm new to swift, so I try to find the best way to store that.

Now I use this code:

var collectionViewData: [collectionViewStruct] = {
    var cell_1 = collectionViewStruct()
    cell_1.title = "..."
    cell_1.text = "..."
    cell_1.firstColor = "C68CF2"
    cell_1.secondColor = "EFA8CA"

    var cell_2 = collectionViewStruct()
    cell_2.title = "..."
    cell_2.text = "..."
    cell_2.firstColor = "C68CF2"
    cell_2.secondColor = "EFA8CA"

    return [cell_1, cell_2]
}()

Is there a way not to write every var in return?

How to return all the variables at once?

Or maybe there is a better way to store this data?

Thanks in Advance :)


Solution

  • If the suggestions data is never changing, just use the following struct:

    struct collectinView {
        let title: String
        let text: String
        let firstColor = "C68CF2"
        let secondColor = "EFA8CA"
    }
    
    let collectionViewData = [
        collectionView(title: "...", text: "..."),
        collectionView(title: "other Title", text: "Other text")]