繁体   English   中英

Swift-计算属性返回

[英]Swift - Computed Property Return

我有一些字典,将用于collectionView中的单元格。 我是新手,所以我尝试找到最佳的存储方式。

现在,我使用以下代码:

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]
}()

有没有办法不写每个变量作为回报?

如何一次返回所有变量?

还是有更好的方法来存储此数据?

提前致谢 :)

为此,您可以使用私有属性,然后将其返回,例如:

    private var _suggestions: [suggestionsStruct] = [suggestionsStruct]()
    var suggestions: [suggestionsStruct] {
    get{

        if _suggestions.count > 0{
            var suggestion_1 = suggestionsStruct()
            suggestion_1.title = "..."
            suggestion_1.text = "..."
            suggestion_1.firstColor = "C68CF2"
            suggestion_1.secondColor = "EFA8CA"
            _suggestions.append(suggestion_1)
        }else{
            var suggestion_1 = suggestionsStruct()
            suggestion_1.title = "..."
            suggestion_1.text = "..."
            suggestion_1.firstColor = "C68CF2"
            suggestion_1.secondColor = "EFA8CA"
            _suggestions = [suggestion_1]
        }
        return _suggestions
    }
}

如果建议数据永远不变,则使用以下结构:

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")]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM