繁体   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