Swift : 协议扩展和数组

Swift : protocol extension and arrays

我有一些像这样异构的结构对象

struct Cat: Hashable {
   let name: String
   let catId: Int
}
struct SubCat: Hashable {
   let name: String
   let catId: Int
   let parentCatId: Int
}

现在我有一个需要显示 Cat 或 SubCat 的 tableView。我的第一选择是用协议扩展两个 classes:

protocol Selectable {
    func asString() -> String
}

我的结构变成了:

struct Cat: Hashable, Selectable {
   let name: String
   let catId: Int
   func asString() -> {
      return self.name
  }
}
struct SubCat: Hashable, Selectable {
   let name: String
   let catId: Int
   let parentCatId: Int
   func asString() -> {
      return self.name
  }
}

到目前为止一切正常。 我在我的 TableViewController 中声明了一个 [Selectable] 对象,使用 asString() 来填充我的单元格。像魅力一样编译。

但事情是这样的。我有一个 CatModel class 和一个 SubCatModel class,每个 returning 一个结构数组 [Cats] 和 [SubCats] 当我尝试将 [Cat] 数组分配给 [Selectable] 数组时,它无法编译。如果我将 [Cat] 数组的 return 类型更改为 [Selectable],它不会编译。

谁能帮我解决这个问题?我想我在这里遗漏了一些东西。 谢谢

Cat 数组映射到 Selectable 数组:

let selectableArray = catArray.map { [=10=] as Selectable }