Swift : protocol extension and arrays

Khal

I have some struct objects that are heterogeneous like this

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

Now I have a tableView that needs to show either Cat or SubCat. My first choice was to extend both classes with a protocol :

protocol Selectable {
    func asString() -> String
}

and my struct became :

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
  }
}

It worked so far. I declared a [Selectable] object in my TableViewController, used asString() to populate my cells. Compiled like a charm.

But here's the thing. I've got a CatModel class and a SubCatModel class, each returning an array of each structs [Cats] and [SubCats] When I try to assign the [Cat] array to the [Selectable] array, it does not compile. If I changed the return type of my [Cat] array to [Selectable], it does not compile.

Can anyone help me with this? I guess I'm missing something here. Thanks.

mixel

Map Cat array to Selectable array:

let selectableArray = catArray.map { $0 as Selectable }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Dispatching to Swift protocol extension

Swift extension that conforms to protocol

Swift: Is it possible to add a protocol extension to a protocol?

Swift protocol extension with property conforming to protocol

Swift: Providing a default protocol implementation in a protocol extension

Swift property observer in protocol extension?

Swift 2: UITableViewDataSource protocol extension

Swift overriding protocol extension keeping the extension behaviour

Swift 3 protocol extension using selector error

Swift 2.2 #selector in protocol extension compiler error

How does protocol extension work in Swift?

Swift protocol extension self reference issues with init

Swift protocol extension with specific Self type

Conforming a generic type to a protocol in a Swift extension

Rust equivalent to Swift's extension methods to a protocol?

How to override computed property of protocol extension in Swift

Invalid Redeclaration of Variable in Swift Protocol Extension

Swift protocol conformance by extension between frameworks

Swift Class Extension Only When Conforming to Protocol

swift 2.0 - UITextFieldDelegate protocol extension not working

Protocol extension method dispatch in Swift 2.0

Swift extension only when conforming to Class AND protocol

Swift protocol extension method dispatch with superclass and subclass

Issue with the call hierarchy of a swift protocol extension

Default implementation of protocol extension in Swift not working

Protocol Extension in Swift Where Object Is a Class and conforms to a Protocol

Swift: Store Type of implementation of Protocol in static var of protocol extension

Swift: converting between Arrays of 'Protocol' and Arrays of implementing Class

Swift Protocol extension: cannot assign to property: '' is a get-only property