Swift - Computed Property Return

MrBlazOn

New Member
#1
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:
Mã:
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 :)
 

Admin

Administrator
Thành viên BQT
#2
If the suggestions data is never changing, just use the following struct:
Mã:
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")]
 
OP
OP
M

MrBlazOn

New Member
#3
Colors are different for each cell, but yeah, I think, this way is much better, thank you! :)
 
Top