Enum associated value confusing
up vote
2
down vote
favorite
When I try use func obj func, I get the error:
Cannot invoke 'obj' with an argument list of type '(message: (QueueAddable))'
I'm confused with Swift types.
Obj func used to get concrete type for decode.
protocol QueueAddable: Encodable
var playlistsCollection:String? get
var playlists: [String]? get
struct Playlist: QueueAddable
var playlistsCollection:String?
return "id"
var playlists: [String]?
return ["id", "id2"]
private enum CodingKeys:String,CodingKey
case playlistsCollection
case playlists
public func encode(to encoder: Encoder) throws
var values = encoder.container(keyedBy: CodingKeys.self)
try values.encode(playlistsCollection, forKey: Playlist.CodingKeys.playlistsCollection)
try values.encode(playlists, forKey: .playlists)
func obj<Q>(message: Q) where Q: QueueAddable
let encoder = JSONEncoder()
let data = try! encoder.encode(message)
enum SomeEnum
case playlist(QueueAddable)
func doSome() throws -> Data
switch self
case .playlist(let queueAddable):
let encoder = JSONEncoder()
// Error on the following line:
obj(message: queueAddable)
return Data()
let playlist = Playlist()
let data = try SomeEnum.playlist(playlist).doSome()
swift enums associated-value
add a comment |
up vote
2
down vote
favorite
When I try use func obj func, I get the error:
Cannot invoke 'obj' with an argument list of type '(message: (QueueAddable))'
I'm confused with Swift types.
Obj func used to get concrete type for decode.
protocol QueueAddable: Encodable
var playlistsCollection:String? get
var playlists: [String]? get
struct Playlist: QueueAddable
var playlistsCollection:String?
return "id"
var playlists: [String]?
return ["id", "id2"]
private enum CodingKeys:String,CodingKey
case playlistsCollection
case playlists
public func encode(to encoder: Encoder) throws
var values = encoder.container(keyedBy: CodingKeys.self)
try values.encode(playlistsCollection, forKey: Playlist.CodingKeys.playlistsCollection)
try values.encode(playlists, forKey: .playlists)
func obj<Q>(message: Q) where Q: QueueAddable
let encoder = JSONEncoder()
let data = try! encoder.encode(message)
enum SomeEnum
case playlist(QueueAddable)
func doSome() throws -> Data
switch self
case .playlist(let queueAddable):
let encoder = JSONEncoder()
// Error on the following line:
obj(message: queueAddable)
return Data()
let playlist = Playlist()
let data = try SomeEnum.playlist(playlist).doSome()
swift enums associated-value
1
objis expecting some type that implementsQueueAddable, but you're sending value that "is of type"QueueAddable. Protocols do not implement themselves, thereforeobjcannot be called withmessage: QueueAddable.
– user28434
Nov 9 at 15:32
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
When I try use func obj func, I get the error:
Cannot invoke 'obj' with an argument list of type '(message: (QueueAddable))'
I'm confused with Swift types.
Obj func used to get concrete type for decode.
protocol QueueAddable: Encodable
var playlistsCollection:String? get
var playlists: [String]? get
struct Playlist: QueueAddable
var playlistsCollection:String?
return "id"
var playlists: [String]?
return ["id", "id2"]
private enum CodingKeys:String,CodingKey
case playlistsCollection
case playlists
public func encode(to encoder: Encoder) throws
var values = encoder.container(keyedBy: CodingKeys.self)
try values.encode(playlistsCollection, forKey: Playlist.CodingKeys.playlistsCollection)
try values.encode(playlists, forKey: .playlists)
func obj<Q>(message: Q) where Q: QueueAddable
let encoder = JSONEncoder()
let data = try! encoder.encode(message)
enum SomeEnum
case playlist(QueueAddable)
func doSome() throws -> Data
switch self
case .playlist(let queueAddable):
let encoder = JSONEncoder()
// Error on the following line:
obj(message: queueAddable)
return Data()
let playlist = Playlist()
let data = try SomeEnum.playlist(playlist).doSome()
swift enums associated-value
When I try use func obj func, I get the error:
Cannot invoke 'obj' with an argument list of type '(message: (QueueAddable))'
I'm confused with Swift types.
Obj func used to get concrete type for decode.
protocol QueueAddable: Encodable
var playlistsCollection:String? get
var playlists: [String]? get
struct Playlist: QueueAddable
var playlistsCollection:String?
return "id"
var playlists: [String]?
return ["id", "id2"]
private enum CodingKeys:String,CodingKey
case playlistsCollection
case playlists
public func encode(to encoder: Encoder) throws
var values = encoder.container(keyedBy: CodingKeys.self)
try values.encode(playlistsCollection, forKey: Playlist.CodingKeys.playlistsCollection)
try values.encode(playlists, forKey: .playlists)
func obj<Q>(message: Q) where Q: QueueAddable
let encoder = JSONEncoder()
let data = try! encoder.encode(message)
enum SomeEnum
case playlist(QueueAddable)
func doSome() throws -> Data
switch self
case .playlist(let queueAddable):
let encoder = JSONEncoder()
// Error on the following line:
obj(message: queueAddable)
return Data()
let playlist = Playlist()
let data = try SomeEnum.playlist(playlist).doSome()
swift enums associated-value
swift enums associated-value
edited Nov 9 at 15:24
rmaddy
234k27305371
234k27305371
asked Nov 9 at 15:06
Drugan
336
336
1
objis expecting some type that implementsQueueAddable, but you're sending value that "is of type"QueueAddable. Protocols do not implement themselves, thereforeobjcannot be called withmessage: QueueAddable.
– user28434
Nov 9 at 15:32
add a comment |
1
objis expecting some type that implementsQueueAddable, but you're sending value that "is of type"QueueAddable. Protocols do not implement themselves, thereforeobjcannot be called withmessage: QueueAddable.
– user28434
Nov 9 at 15:32
1
1
obj is expecting some type that implements QueueAddable, but you're sending value that "is of type" QueueAddable. Protocols do not implement themselves, therefore obj cannot be called with message: QueueAddable.– user28434
Nov 9 at 15:32
obj is expecting some type that implements QueueAddable, but you're sending value that "is of type" QueueAddable. Protocols do not implement themselves, therefore obj cannot be called with message: QueueAddable.– user28434
Nov 9 at 15:32
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
I think the problem is that the function expects a type and not a protocol. If you make the enum generic using a type that implements the protocol it will work.
Change the enum's first two lines like this:
enum SomeEnum<Q : QueueAddable> {
case playlist(Q)
I tested in the following playground:
import Foundation
protocol QueueAddable: Encodable
var playlistsCollection:String? get
var playlists: [String]? get
struct Playlist: QueueAddable
var playlistsCollection:String?
return "id"
var playlists: [String]?
return ["id", "id2"]
private enum CodingKeys:String,CodingKey
case playlistsCollection
case playlists
public func encode(to encoder: Encoder) throws
var values = encoder.container(keyedBy: CodingKeys.self)
try values.encode(playlistsCollection, forKey: Playlist.CodingKeys.playlistsCollection)
try values.encode(playlists, forKey: .playlists)
func obj<Q>(message: Q) where Q: QueueAddable
let encoder = JSONEncoder()
let data = try! encoder.encode(message)
enum SomeEnum<Q : QueueAddable>
case playlist(Q)
func doSome() throws -> Data
switch self
case .playlist(let queueAddable):
let encoder = JSONEncoder()
// No longer error on the following line:
obj(message: queueAddable)
return Data()
let playlist = Playlist()
let data = try SomeEnum.playlist(playlist).doSome()
Hope it helps!
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
I think the problem is that the function expects a type and not a protocol. If you make the enum generic using a type that implements the protocol it will work.
Change the enum's first two lines like this:
enum SomeEnum<Q : QueueAddable> {
case playlist(Q)
I tested in the following playground:
import Foundation
protocol QueueAddable: Encodable
var playlistsCollection:String? get
var playlists: [String]? get
struct Playlist: QueueAddable
var playlistsCollection:String?
return "id"
var playlists: [String]?
return ["id", "id2"]
private enum CodingKeys:String,CodingKey
case playlistsCollection
case playlists
public func encode(to encoder: Encoder) throws
var values = encoder.container(keyedBy: CodingKeys.self)
try values.encode(playlistsCollection, forKey: Playlist.CodingKeys.playlistsCollection)
try values.encode(playlists, forKey: .playlists)
func obj<Q>(message: Q) where Q: QueueAddable
let encoder = JSONEncoder()
let data = try! encoder.encode(message)
enum SomeEnum<Q : QueueAddable>
case playlist(Q)
func doSome() throws -> Data
switch self
case .playlist(let queueAddable):
let encoder = JSONEncoder()
// No longer error on the following line:
obj(message: queueAddable)
return Data()
let playlist = Playlist()
let data = try SomeEnum.playlist(playlist).doSome()
Hope it helps!
add a comment |
up vote
1
down vote
accepted
I think the problem is that the function expects a type and not a protocol. If you make the enum generic using a type that implements the protocol it will work.
Change the enum's first two lines like this:
enum SomeEnum<Q : QueueAddable> {
case playlist(Q)
I tested in the following playground:
import Foundation
protocol QueueAddable: Encodable
var playlistsCollection:String? get
var playlists: [String]? get
struct Playlist: QueueAddable
var playlistsCollection:String?
return "id"
var playlists: [String]?
return ["id", "id2"]
private enum CodingKeys:String,CodingKey
case playlistsCollection
case playlists
public func encode(to encoder: Encoder) throws
var values = encoder.container(keyedBy: CodingKeys.self)
try values.encode(playlistsCollection, forKey: Playlist.CodingKeys.playlistsCollection)
try values.encode(playlists, forKey: .playlists)
func obj<Q>(message: Q) where Q: QueueAddable
let encoder = JSONEncoder()
let data = try! encoder.encode(message)
enum SomeEnum<Q : QueueAddable>
case playlist(Q)
func doSome() throws -> Data
switch self
case .playlist(let queueAddable):
let encoder = JSONEncoder()
// No longer error on the following line:
obj(message: queueAddable)
return Data()
let playlist = Playlist()
let data = try SomeEnum.playlist(playlist).doSome()
Hope it helps!
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
I think the problem is that the function expects a type and not a protocol. If you make the enum generic using a type that implements the protocol it will work.
Change the enum's first two lines like this:
enum SomeEnum<Q : QueueAddable> {
case playlist(Q)
I tested in the following playground:
import Foundation
protocol QueueAddable: Encodable
var playlistsCollection:String? get
var playlists: [String]? get
struct Playlist: QueueAddable
var playlistsCollection:String?
return "id"
var playlists: [String]?
return ["id", "id2"]
private enum CodingKeys:String,CodingKey
case playlistsCollection
case playlists
public func encode(to encoder: Encoder) throws
var values = encoder.container(keyedBy: CodingKeys.self)
try values.encode(playlistsCollection, forKey: Playlist.CodingKeys.playlistsCollection)
try values.encode(playlists, forKey: .playlists)
func obj<Q>(message: Q) where Q: QueueAddable
let encoder = JSONEncoder()
let data = try! encoder.encode(message)
enum SomeEnum<Q : QueueAddable>
case playlist(Q)
func doSome() throws -> Data
switch self
case .playlist(let queueAddable):
let encoder = JSONEncoder()
// No longer error on the following line:
obj(message: queueAddable)
return Data()
let playlist = Playlist()
let data = try SomeEnum.playlist(playlist).doSome()
Hope it helps!
I think the problem is that the function expects a type and not a protocol. If you make the enum generic using a type that implements the protocol it will work.
Change the enum's first two lines like this:
enum SomeEnum<Q : QueueAddable> {
case playlist(Q)
I tested in the following playground:
import Foundation
protocol QueueAddable: Encodable
var playlistsCollection:String? get
var playlists: [String]? get
struct Playlist: QueueAddable
var playlistsCollection:String?
return "id"
var playlists: [String]?
return ["id", "id2"]
private enum CodingKeys:String,CodingKey
case playlistsCollection
case playlists
public func encode(to encoder: Encoder) throws
var values = encoder.container(keyedBy: CodingKeys.self)
try values.encode(playlistsCollection, forKey: Playlist.CodingKeys.playlistsCollection)
try values.encode(playlists, forKey: .playlists)
func obj<Q>(message: Q) where Q: QueueAddable
let encoder = JSONEncoder()
let data = try! encoder.encode(message)
enum SomeEnum<Q : QueueAddable>
case playlist(Q)
func doSome() throws -> Data
switch self
case .playlist(let queueAddable):
let encoder = JSONEncoder()
// No longer error on the following line:
obj(message: queueAddable)
return Data()
let playlist = Playlist()
let data = try SomeEnum.playlist(playlist).doSome()
Hope it helps!
answered 2 days ago
acyrman
1166
1166
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53228267%2fenum-associated-value-confusing%23new-answer', 'question_page');
);
Post as a guest
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
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
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
1
objis expecting some type that implementsQueueAddable, but you're sending value that "is of type"QueueAddable. Protocols do not implement themselves, thereforeobjcannot be called withmessage: QueueAddable.– user28434
Nov 9 at 15:32