Swift - Inheriting UIViewController from a generic base class that is also a UIViewController
up vote
0
down vote
favorite
So I have a base class, DataViewController<T>
, which inherits from UITableViewController
as follows:
class DataTableViewController<T : BMPage<BMData>> : UITableViewController, UISearchBarDelegate
// implementations...
I also have a TournamentsViewController
which inherits from DataViewController
with the generics passed in:
class TournamentsViewController: DataTableViewController<TournamentPage>
// overrides, etc.
For reference, here are BMPage<T>
:
open class BMPage<T : Codable> : Decodable
public enum CodingKeys: String, CodingKey
case data
var data : [T]
required public init(from decoder:Decoder) throws
let vals = try decoder.container(keyedBy: CodingKeys.self)
data = try vals.decode([T].self, forKey: .data)
extension BMPage: Encodable
public func encode(to encoder: Encoder) throws
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(data, forKey: .data)
... and BMData
:
open class BMData : Codable
public enum CodingKeys: CodingKey
case id
case name
var id: Int
var name: String
required public init(from decoder:Decoder) throws
let vals = try decoder.container(keyedBy: CodingKeys.self)
id = try vals.decode(Int.self, forKey: .id)
name = try vals.decode(String.self, forKey: .name)
What I'm trying to achieve: various UITableViewController
subclasses that just need to specify generics in order to specify their specific data. Here are my Tournament and TournamentPage implementations which should better explain this concept:
open class Tournament : BMData
let game_id: Int = 0
let game_iteration_id: Int = 0
let state: Int = 0
let starts_at: String = ""
let creator_id: Int = 0
let stream_url: String? = nil
let entrant_count: Int = 0
let prereg_count: Int = 0
let path: String = ""
open class TournamentPage : BMPage<BMData>
public enum CodingKeys: String, CodingKey
case data = "tournaments"
let page: String? = nil
let results_per_page: String = ""
let tournament_count: Int = 0
The problem: the compiler doesn't like that I'm doing this as it's throwing multiple, yet very similar errors such as:
Cannot convert return expression of type 'TournamentsViewController'
to return type 'UIViewController'Cannot convert return expression of type
'TournamentsViewController?' to return type 'UIViewController?'
Here is an example of such an expression (taken from the default ModelController
that XCode threw in there, which is where all of these errors occur:
let dataViewController = storyboard.instantiateViewController(withIdentifier: "DataViewController") as! TournamentsViewController
Is there a way to achieve what I am trying to do? I'll admit, I am new to Swift (only started learning it this week), so maybe I'm completely misunderstanding typical Swift practices and just what it was designed to do, but achieving this in a language such as C# (and probably even Objective-C, though I'm a bit rusty on it as it's been a while since I've last used it) is pretty much what I'm doing here.
ios swift oop generics inheritance
add a comment |
up vote
0
down vote
favorite
So I have a base class, DataViewController<T>
, which inherits from UITableViewController
as follows:
class DataTableViewController<T : BMPage<BMData>> : UITableViewController, UISearchBarDelegate
// implementations...
I also have a TournamentsViewController
which inherits from DataViewController
with the generics passed in:
class TournamentsViewController: DataTableViewController<TournamentPage>
// overrides, etc.
For reference, here are BMPage<T>
:
open class BMPage<T : Codable> : Decodable
public enum CodingKeys: String, CodingKey
case data
var data : [T]
required public init(from decoder:Decoder) throws
let vals = try decoder.container(keyedBy: CodingKeys.self)
data = try vals.decode([T].self, forKey: .data)
extension BMPage: Encodable
public func encode(to encoder: Encoder) throws
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(data, forKey: .data)
... and BMData
:
open class BMData : Codable
public enum CodingKeys: CodingKey
case id
case name
var id: Int
var name: String
required public init(from decoder:Decoder) throws
let vals = try decoder.container(keyedBy: CodingKeys.self)
id = try vals.decode(Int.self, forKey: .id)
name = try vals.decode(String.self, forKey: .name)
What I'm trying to achieve: various UITableViewController
subclasses that just need to specify generics in order to specify their specific data. Here are my Tournament and TournamentPage implementations which should better explain this concept:
open class Tournament : BMData
let game_id: Int = 0
let game_iteration_id: Int = 0
let state: Int = 0
let starts_at: String = ""
let creator_id: Int = 0
let stream_url: String? = nil
let entrant_count: Int = 0
let prereg_count: Int = 0
let path: String = ""
open class TournamentPage : BMPage<BMData>
public enum CodingKeys: String, CodingKey
case data = "tournaments"
let page: String? = nil
let results_per_page: String = ""
let tournament_count: Int = 0
The problem: the compiler doesn't like that I'm doing this as it's throwing multiple, yet very similar errors such as:
Cannot convert return expression of type 'TournamentsViewController'
to return type 'UIViewController'Cannot convert return expression of type
'TournamentsViewController?' to return type 'UIViewController?'
Here is an example of such an expression (taken from the default ModelController
that XCode threw in there, which is where all of these errors occur:
let dataViewController = storyboard.instantiateViewController(withIdentifier: "DataViewController") as! TournamentsViewController
Is there a way to achieve what I am trying to do? I'll admit, I am new to Swift (only started learning it this week), so maybe I'm completely misunderstanding typical Swift practices and just what it was designed to do, but achieving this in a language such as C# (and probably even Objective-C, though I'm a bit rusty on it as it's been a while since I've last used it) is pretty much what I'm doing here.
ios swift oop generics inheritance
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
So I have a base class, DataViewController<T>
, which inherits from UITableViewController
as follows:
class DataTableViewController<T : BMPage<BMData>> : UITableViewController, UISearchBarDelegate
// implementations...
I also have a TournamentsViewController
which inherits from DataViewController
with the generics passed in:
class TournamentsViewController: DataTableViewController<TournamentPage>
// overrides, etc.
For reference, here are BMPage<T>
:
open class BMPage<T : Codable> : Decodable
public enum CodingKeys: String, CodingKey
case data
var data : [T]
required public init(from decoder:Decoder) throws
let vals = try decoder.container(keyedBy: CodingKeys.self)
data = try vals.decode([T].self, forKey: .data)
extension BMPage: Encodable
public func encode(to encoder: Encoder) throws
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(data, forKey: .data)
... and BMData
:
open class BMData : Codable
public enum CodingKeys: CodingKey
case id
case name
var id: Int
var name: String
required public init(from decoder:Decoder) throws
let vals = try decoder.container(keyedBy: CodingKeys.self)
id = try vals.decode(Int.self, forKey: .id)
name = try vals.decode(String.self, forKey: .name)
What I'm trying to achieve: various UITableViewController
subclasses that just need to specify generics in order to specify their specific data. Here are my Tournament and TournamentPage implementations which should better explain this concept:
open class Tournament : BMData
let game_id: Int = 0
let game_iteration_id: Int = 0
let state: Int = 0
let starts_at: String = ""
let creator_id: Int = 0
let stream_url: String? = nil
let entrant_count: Int = 0
let prereg_count: Int = 0
let path: String = ""
open class TournamentPage : BMPage<BMData>
public enum CodingKeys: String, CodingKey
case data = "tournaments"
let page: String? = nil
let results_per_page: String = ""
let tournament_count: Int = 0
The problem: the compiler doesn't like that I'm doing this as it's throwing multiple, yet very similar errors such as:
Cannot convert return expression of type 'TournamentsViewController'
to return type 'UIViewController'Cannot convert return expression of type
'TournamentsViewController?' to return type 'UIViewController?'
Here is an example of such an expression (taken from the default ModelController
that XCode threw in there, which is where all of these errors occur:
let dataViewController = storyboard.instantiateViewController(withIdentifier: "DataViewController") as! TournamentsViewController
Is there a way to achieve what I am trying to do? I'll admit, I am new to Swift (only started learning it this week), so maybe I'm completely misunderstanding typical Swift practices and just what it was designed to do, but achieving this in a language such as C# (and probably even Objective-C, though I'm a bit rusty on it as it's been a while since I've last used it) is pretty much what I'm doing here.
ios swift oop generics inheritance
So I have a base class, DataViewController<T>
, which inherits from UITableViewController
as follows:
class DataTableViewController<T : BMPage<BMData>> : UITableViewController, UISearchBarDelegate
// implementations...
I also have a TournamentsViewController
which inherits from DataViewController
with the generics passed in:
class TournamentsViewController: DataTableViewController<TournamentPage>
// overrides, etc.
For reference, here are BMPage<T>
:
open class BMPage<T : Codable> : Decodable
public enum CodingKeys: String, CodingKey
case data
var data : [T]
required public init(from decoder:Decoder) throws
let vals = try decoder.container(keyedBy: CodingKeys.self)
data = try vals.decode([T].self, forKey: .data)
extension BMPage: Encodable
public func encode(to encoder: Encoder) throws
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(data, forKey: .data)
... and BMData
:
open class BMData : Codable
public enum CodingKeys: CodingKey
case id
case name
var id: Int
var name: String
required public init(from decoder:Decoder) throws
let vals = try decoder.container(keyedBy: CodingKeys.self)
id = try vals.decode(Int.self, forKey: .id)
name = try vals.decode(String.self, forKey: .name)
What I'm trying to achieve: various UITableViewController
subclasses that just need to specify generics in order to specify their specific data. Here are my Tournament and TournamentPage implementations which should better explain this concept:
open class Tournament : BMData
let game_id: Int = 0
let game_iteration_id: Int = 0
let state: Int = 0
let starts_at: String = ""
let creator_id: Int = 0
let stream_url: String? = nil
let entrant_count: Int = 0
let prereg_count: Int = 0
let path: String = ""
open class TournamentPage : BMPage<BMData>
public enum CodingKeys: String, CodingKey
case data = "tournaments"
let page: String? = nil
let results_per_page: String = ""
let tournament_count: Int = 0
The problem: the compiler doesn't like that I'm doing this as it's throwing multiple, yet very similar errors such as:
Cannot convert return expression of type 'TournamentsViewController'
to return type 'UIViewController'Cannot convert return expression of type
'TournamentsViewController?' to return type 'UIViewController?'
Here is an example of such an expression (taken from the default ModelController
that XCode threw in there, which is where all of these errors occur:
let dataViewController = storyboard.instantiateViewController(withIdentifier: "DataViewController") as! TournamentsViewController
Is there a way to achieve what I am trying to do? I'll admit, I am new to Swift (only started learning it this week), so maybe I'm completely misunderstanding typical Swift practices and just what it was designed to do, but achieving this in a language such as C# (and probably even Objective-C, though I'm a bit rusty on it as it's been a while since I've last used it) is pretty much what I'm doing here.
ios swift oop generics inheritance
ios swift oop generics inheritance
edited Nov 9 at 23:55
asked Nov 9 at 23:49
tayoung
101110
101110
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
Have you tried to instantiate view controller like this?let viewController = MyTableViewController()
Than navigationController.pushViewController(viewController, animated: true)
}
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Have you tried to instantiate view controller like this?let viewController = MyTableViewController()
Than navigationController.pushViewController(viewController, animated: true)
}
add a comment |
up vote
0
down vote
Have you tried to instantiate view controller like this?let viewController = MyTableViewController()
Than navigationController.pushViewController(viewController, animated: true)
}
add a comment |
up vote
0
down vote
up vote
0
down vote
Have you tried to instantiate view controller like this?let viewController = MyTableViewController()
Than navigationController.pushViewController(viewController, animated: true)
}
Have you tried to instantiate view controller like this?let viewController = MyTableViewController()
Than navigationController.pushViewController(viewController, animated: true)
}
answered Nov 10 at 0:16
Alastar
524
524
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53234700%2fswift-inheriting-uiviewcontroller-from-a-generic-base-class-that-is-also-a-uiv%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown