Swift 4将项目附加到结构化数组

时间:2018-03-15 18:28:29

标签: arrays swift append swift4

我希望(根据此示例)在创建初始条目后向结构化数组的特定部分添加更多项。

struct Zoo {
    let section: String
    let items: [String]
}

var objects = [Zoo]()

let animals = Zoo(section: "Animals", items: ["Cat","Dog","Mouse"])
let birds = Zoo(section: "Birds", items: ["Crow","Pidgeon","Hawk"])

let reptiles = ["Snake","Lizard"]

objects.append(animals)
objects.append(birds)

// ... varous logic and proccessing where I determine I need 
// to add two more items to the animals section...

// trying to extend animals with two more entries.
// this is where I am getting hung up:
objects[0].items.append(reptiles)

1 个答案:

答案 0 :(得分:0)

删除以下代码

  objects[0].items.append(reptiles)

使用此代码:

objects[0].items +=  reptiles