簡體   English   中英

如何從具有結構類型的數組中調用變量?

[英]How can I call a variable from an array that has a type of struct?

如何調用數組內的圖像變量?

static let data: [Meditation] = [
        Meditation(title: "1 Minute Relaxing Meditation", description: "Clear your mind and slumber into nothingness. Allocate only a few moments for a quick breather.", duration: 70, track: "meditation1", image: "Ambient"),
        Meditation(title: "1 Minute Relaxing Meditation", description: "Clear your mind and slumber into nothingness. Allocate only a few moments for a quick breather.", duration: 70, track: "meditation1", image: "World"),
        Meditation(title: "1 Minute Relaxing Meditation", description: "Clear your mind and slumber into nothingness. Allocate only a few moments for a quick breather.", duration: 70, track: "meditation1", image: "Pop"),
        Meditation(title: "1 Minute Relaxing Meditation", description: "Clear your mind and slumber into nothingness. Allocate only a few moments for a quick breather.", duration: 70, track: "meditation1", image: "HipHop"),
        Meditation(title: "1 Minute Relaxing Meditation", description: "Clear your mind and slumber into nothingness. Allocate only a few moments for a quick breather.", duration: 70, track: "meditation1", image: "R&B"),
        Meditation(title: "1 Minute Relaxing Meditation", description: "Clear your mind and slumber into nothingness. Allocate only a few moments for a quick breather.", duration: 70, track: "meditation1", image: "EDM")
    ]

我努力了; 但是,它不起作用。

meditation.data.image

由於 data 是一個array ,你不能只調用.image 你必須知道你想要哪個元素的圖像然后訪問它。

如果你想要第一個元素,只需調用:

meditation.data.first?.image

如果沒有,您需要filter數組或使用索引來獲取您想要的元素:

meditation.data[3].image//fourth image

作為數據變量是一個冥想類型的數組。

為了訪問數組中的值,我們需要使用下面給出的索引值:

對於第 0 個索引,我們可以將 image 訪問為冥想.data[0].image // Ambient

對於第一個索引,我們可以將 image 訪問為冥想.data[1].image // World

對於第二個索引,我們可以訪問圖像作為冥想.data[2].image // Pop

對於第 3 個索引,我們可以將 image 訪問為冥想.data[3].image // HipHop

對於第 4 個索引,我們可以將 image 訪問為冥想.data[4].image // R&B

對於第 5 個索引,我們可以將 image 訪問為冥想.data[5].image // EDM

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM