如何访问结构 Swift 中的结构数组

How to acces array of structures in structure Swift

我的结构如下:

struct Points : Codable {
    var name : String?
    var interests : [Interests]
    
    ///How do i get string or array of string that is equal to all interests
    
    var allInterestText : String ///???
}

struct Interests : Codable {
    var interest : Interest?
}

struct Interest : Codable{
    var name : String?
}

我一直在努力实现这一点,但我所有的尝试都失败了。

对单个字符串试试这个:

struct Points {

    // ...

    var allInterestText: String {
        interests.compactMap { [=10=].interest?.name }.joined(separator: " ")
    }
}

如果您决定改用数组,只需更改类型并删除 .joined()