[英]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.