Ezra
Ezra

Reputation: 670

Swift 3 Generic Type usage

First, I have a protocol:

public protocol ContentList {
//...
}

and some structs:

public struct Post: ContentList {
//...
}
public struct Essay: ContentList {
//...
}

then I have a subclass of UITableViewController:

open class MainTableViewController<T: ContentList>: UITableViewController {
    // ...
    func getData() -> [T] {
        retrun ...
    }
}

the UI part of this class, MainTableViewController, was defined in Main.storyboard, so I did this:

let kMainTableViewControllerIdentifier = "MainTableViewController"
let listController = (self.storyboard.instantiateViewController(withIdentifier: kMainTableViewControllerIdentifier) as? MainTableViewController<Post>)!
listController.dataSourceType = .post

and I got some error:

Unknown class _TtC5Elias23MainTableViewController in Interface Builder file.
Could not cast value of type 'UITableViewController' (0x1af463db8) to 'Elias.MainTableViewController<Elias.Post>' (0x101907af8).

 Could not cast value of type 'UITableViewController' (0x1af463db8) to 'Elias.MainTableViewController<Elias.Post>' (0x101907af8).

I have no ideas about how to solve this so far. so I need some help.

Upvotes: 0

Views: 74

Answers (1)

Incinerator
Incinerator

Reputation: 2817

Make sure your view controller is defined as a MainTableViewController in the storyboard as well!

enter image description here

Upvotes: 1

Related Questions