简体   繁体   English

Swift 3通用类型用法

[英]Swift 3 Generic Type usage

First, I have a protocol: 首先,我有一个协议:

public protocol ContentList {
//...
}

and some struct s: 和一些struct

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

then I have a subclass of UITableViewController : 然后我有一个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: 此类的UI部分MainTableViewController是在Main.storyboard中定义的,所以我这样做了:

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. 所以我需要一些帮助。

Make sure your view controller is defined as a MainTableViewController in the storyboard as well! 确保在情节提要中将视图控制器也定义为MainTableViewController!

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM