How to implement Codable while using Realm
Hy I am working on app that uses Realm and Alamofire. I am really happy in using these library in my iOS project.
But then I have to post a List of models that contains multiple lists of models. So that is too much deep thing I mean List inside List that contains models and those model contains list of several model
For demonstration lets just take an example of my models
@objcMembers public class MyModel : Object{
dynamic var Id: String = ""
dynamic var Name: String = ""
dynamic var Favorites: List<String>? = nil
dynamic var Subjects: List<UserSubject>? = nil
}
@objcMembers public class UserSubject: Object{
dynamic var Id: String = ""
dynamic var Name: String = ""
dynamic var Teachers: List<Publications>? = nil
}
@objcMembers public class Publications: Object{
dynamic var Id: String = ""
dynamic var Name: String = ""
dynamic var Editors: List<Editors>? = nil
}
So you can see these are models inside list that contains another list of model.
Due to Realm I am using List for list for creating the RelationShip.
Problem: but Now when I tries to implement Codable on Models/Struct It really unable to recognize List property.
I really do not know how to solve this problem? Do anyone have any Idea how to do it ?
UPDATE:
I am using Swift 4.0 and base sdk is 11.2
ios realm swift4 alamofire codable
add a comment |
Hy I am working on app that uses Realm and Alamofire. I am really happy in using these library in my iOS project.
But then I have to post a List of models that contains multiple lists of models. So that is too much deep thing I mean List inside List that contains models and those model contains list of several model
For demonstration lets just take an example of my models
@objcMembers public class MyModel : Object{
dynamic var Id: String = ""
dynamic var Name: String = ""
dynamic var Favorites: List<String>? = nil
dynamic var Subjects: List<UserSubject>? = nil
}
@objcMembers public class UserSubject: Object{
dynamic var Id: String = ""
dynamic var Name: String = ""
dynamic var Teachers: List<Publications>? = nil
}
@objcMembers public class Publications: Object{
dynamic var Id: String = ""
dynamic var Name: String = ""
dynamic var Editors: List<Editors>? = nil
}
So you can see these are models inside list that contains another list of model.
Due to Realm I am using List for list for creating the RelationShip.
Problem: but Now when I tries to implement Codable on Models/Struct It really unable to recognize List property.
I really do not know how to solve this problem? Do anyone have any Idea how to do it ?
UPDATE:
I am using Swift 4.0 and base sdk is 11.2
ios realm swift4 alamofire codable
add a comment |
Hy I am working on app that uses Realm and Alamofire. I am really happy in using these library in my iOS project.
But then I have to post a List of models that contains multiple lists of models. So that is too much deep thing I mean List inside List that contains models and those model contains list of several model
For demonstration lets just take an example of my models
@objcMembers public class MyModel : Object{
dynamic var Id: String = ""
dynamic var Name: String = ""
dynamic var Favorites: List<String>? = nil
dynamic var Subjects: List<UserSubject>? = nil
}
@objcMembers public class UserSubject: Object{
dynamic var Id: String = ""
dynamic var Name: String = ""
dynamic var Teachers: List<Publications>? = nil
}
@objcMembers public class Publications: Object{
dynamic var Id: String = ""
dynamic var Name: String = ""
dynamic var Editors: List<Editors>? = nil
}
So you can see these are models inside list that contains another list of model.
Due to Realm I am using List for list for creating the RelationShip.
Problem: but Now when I tries to implement Codable on Models/Struct It really unable to recognize List property.
I really do not know how to solve this problem? Do anyone have any Idea how to do it ?
UPDATE:
I am using Swift 4.0 and base sdk is 11.2
ios realm swift4 alamofire codable
Hy I am working on app that uses Realm and Alamofire. I am really happy in using these library in my iOS project.
But then I have to post a List of models that contains multiple lists of models. So that is too much deep thing I mean List inside List that contains models and those model contains list of several model
For demonstration lets just take an example of my models
@objcMembers public class MyModel : Object{
dynamic var Id: String = ""
dynamic var Name: String = ""
dynamic var Favorites: List<String>? = nil
dynamic var Subjects: List<UserSubject>? = nil
}
@objcMembers public class UserSubject: Object{
dynamic var Id: String = ""
dynamic var Name: String = ""
dynamic var Teachers: List<Publications>? = nil
}
@objcMembers public class Publications: Object{
dynamic var Id: String = ""
dynamic var Name: String = ""
dynamic var Editors: List<Editors>? = nil
}
So you can see these are models inside list that contains another list of model.
Due to Realm I am using List for list for creating the RelationShip.
Problem: but Now when I tries to implement Codable on Models/Struct It really unable to recognize List property.
I really do not know how to solve this problem? Do anyone have any Idea how to do it ?
UPDATE:
I am using Swift 4.0 and base sdk is 11.2
ios realm swift4 alamofire codable
ios realm swift4 alamofire codable
edited Nov 26 '18 at 12:45
Shararti KAKI
asked Nov 16 '18 at 6:44
Shararti KAKIShararti KAKI
860422
860422
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
Try this:
extension List: Decodable where Element: Decodable {
public convenience init(from decoder: Decoder) throws {
self.init()
var container = try decoder.unkeyedContainer()
while !container.isAtEnd {
let element = try container.decode(Element.self)
self.append(element)
}
}
}
extension List: Encodable where Element: Encodable {
public func encode(to encoder: Encoder) throws {
var container = encoder.unkeyedContainer()
for element in self {
try element.encode(to: container.superEncoder())
}
}
}
Found it here: How to use List type with Codable? (RealmSwift)
it gives me error "Extension of type 'List' with constraints cannot have an inheritance clause"
– Shararti KAKI
Nov 26 '18 at 5:03
Are you importing "Realm" or "RealmSwift"?
– André
Nov 26 '18 at 11:55
I am importing RealmSwift
– Shararti KAKI
Nov 26 '18 at 12:39
Do you have any other List extensions? Is it in a separated file with RealmSwift imported? Did you try cleaning the project before building? Sorry to ask, but I remember seeing this error when I tried to implement this extension and it was solved under these conditions!
– André
Nov 28 '18 at 0:49
No I have not a single extension related to RealmSwift
– Shararti KAKI
Nov 28 '18 at 4:52
add a comment |
You can use extensions for List
Swift 4.1
extension List: Decodable where Element: Decodable {
public convenience init(from decoder: Decoder) throws {
// Initialize self here so we can get type(of: self).
self.init()
assertTypeIsDecodable(Element.self, in: type(of: self))
let metaType = (Element.self as Decodable.Type) // swiftlint:disable:this force_cast
var container = try decoder.unkeyedContainer()
while !container.isAtEnd {
let element = try metaType.init(__from: &container)
self.append(element as! Element) // swiftlint:disable:this force_cast
}
}
}
extension List: Encodable where Element: Decodable {
public func encode(to encoder: Encoder) throws {
assertTypeIsEncodable(Element.self, in: type(of: self))
var container = encoder.unkeyedContainer()
for element in self {
// superEncoder appends an empty element and wraps an Encoder around it.
// This is normally appropriate for encoding super, but this is really what we want to do.
let subencoder = container.superEncoder()
try (element as! Encodable).encode(to: subencoder) // swiftlint:disable:this force_cast
}
}
}
it is error prone. first error it is giving on extension line is "Extension of type List with constraints can not have an inheritence clause"
– Shararti KAKI
Nov 19 '18 at 5:02
Above Code Snippet is for Swift 4+
– Sushil
Nov 22 '18 at 19:50
1
This uses conditional conformances which were added in Swift 4.1
– Sven
Nov 22 '18 at 20:44
Yes, Thank you Sven
– Sushil
Nov 22 '18 at 23:11
it gives me following errors 1> Extention of type 'List' with constraints cannot have an inheritance clause 2> Used of unresolved identifier 'assertTypeIsDecodable' 3> Incorrect argument label in call (have '__form; expected 'from:')
– Shararti KAKI
Nov 26 '18 at 5:46
|
show 2 more comments
Had the same problem in a project and wrote these extensions:
import Foundation
import RealmSwift
extension RealmSwift.List: Decodable where Element: Decodable {
public convenience init(from decoder: Decoder) throws {
self.init()
let container = try decoder.singleValueContainer()
let decodedElements = try container.decode([Element].self)
self.append(objectsIn: decodedElements)
}
}
extension RealmSwift.List: Encodable where Element: Encodable {
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(self.map { $0 })
}
}
With these extension you easily can make the Realm Object Codable. Like this
@objcMembers public class MyModel: Object, Codable {
dynamic var id: String = ""
dynamic var name: String = ""
var favorites = List<String>()
var subjects = List<UserSubject>()
}
@objcMembers public class UserSubject: Object, Codable {
dynamic var id: String = ""
dynamic var name: String = ""
var teachers = List<Publications>()
}
@objcMembers public class Publications: Object, Codable {
dynamic var id: String = ""
dynamic var name: String = ""
var editors = List<Editor>()
}
@objcMembers public class Editor: Object, Codable {
}
it gives me error "Extension of type 'List' with constraints cannot have an inheritance clause"
– Shararti KAKI
Nov 26 '18 at 5:44
Properties which are aList<T>
do not have to be dynamic. Did you make all elements implement theCodable
protocol?
– Oliver
Nov 28 '18 at 8:15
I do not know how to implement it that is main problem
– Shararti KAKI
Nov 28 '18 at 13:30
I updated my answer which is now containing your classes with theCodable
protocol. Actually i's very easy: When you make a classCodable
all it members have to beCodable
as well.
– Oliver
Nov 28 '18 at 17:35
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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%2f53332732%2fhow-to-implement-codable-while-using-realm%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try this:
extension List: Decodable where Element: Decodable {
public convenience init(from decoder: Decoder) throws {
self.init()
var container = try decoder.unkeyedContainer()
while !container.isAtEnd {
let element = try container.decode(Element.self)
self.append(element)
}
}
}
extension List: Encodable where Element: Encodable {
public func encode(to encoder: Encoder) throws {
var container = encoder.unkeyedContainer()
for element in self {
try element.encode(to: container.superEncoder())
}
}
}
Found it here: How to use List type with Codable? (RealmSwift)
it gives me error "Extension of type 'List' with constraints cannot have an inheritance clause"
– Shararti KAKI
Nov 26 '18 at 5:03
Are you importing "Realm" or "RealmSwift"?
– André
Nov 26 '18 at 11:55
I am importing RealmSwift
– Shararti KAKI
Nov 26 '18 at 12:39
Do you have any other List extensions? Is it in a separated file with RealmSwift imported? Did you try cleaning the project before building? Sorry to ask, but I remember seeing this error when I tried to implement this extension and it was solved under these conditions!
– André
Nov 28 '18 at 0:49
No I have not a single extension related to RealmSwift
– Shararti KAKI
Nov 28 '18 at 4:52
add a comment |
Try this:
extension List: Decodable where Element: Decodable {
public convenience init(from decoder: Decoder) throws {
self.init()
var container = try decoder.unkeyedContainer()
while !container.isAtEnd {
let element = try container.decode(Element.self)
self.append(element)
}
}
}
extension List: Encodable where Element: Encodable {
public func encode(to encoder: Encoder) throws {
var container = encoder.unkeyedContainer()
for element in self {
try element.encode(to: container.superEncoder())
}
}
}
Found it here: How to use List type with Codable? (RealmSwift)
it gives me error "Extension of type 'List' with constraints cannot have an inheritance clause"
– Shararti KAKI
Nov 26 '18 at 5:03
Are you importing "Realm" or "RealmSwift"?
– André
Nov 26 '18 at 11:55
I am importing RealmSwift
– Shararti KAKI
Nov 26 '18 at 12:39
Do you have any other List extensions? Is it in a separated file with RealmSwift imported? Did you try cleaning the project before building? Sorry to ask, but I remember seeing this error when I tried to implement this extension and it was solved under these conditions!
– André
Nov 28 '18 at 0:49
No I have not a single extension related to RealmSwift
– Shararti KAKI
Nov 28 '18 at 4:52
add a comment |
Try this:
extension List: Decodable where Element: Decodable {
public convenience init(from decoder: Decoder) throws {
self.init()
var container = try decoder.unkeyedContainer()
while !container.isAtEnd {
let element = try container.decode(Element.self)
self.append(element)
}
}
}
extension List: Encodable where Element: Encodable {
public func encode(to encoder: Encoder) throws {
var container = encoder.unkeyedContainer()
for element in self {
try element.encode(to: container.superEncoder())
}
}
}
Found it here: How to use List type with Codable? (RealmSwift)
Try this:
extension List: Decodable where Element: Decodable {
public convenience init(from decoder: Decoder) throws {
self.init()
var container = try decoder.unkeyedContainer()
while !container.isAtEnd {
let element = try container.decode(Element.self)
self.append(element)
}
}
}
extension List: Encodable where Element: Encodable {
public func encode(to encoder: Encoder) throws {
var container = encoder.unkeyedContainer()
for element in self {
try element.encode(to: container.superEncoder())
}
}
}
Found it here: How to use List type with Codable? (RealmSwift)
answered Nov 22 '18 at 18:46
AndréAndré
1212
1212
it gives me error "Extension of type 'List' with constraints cannot have an inheritance clause"
– Shararti KAKI
Nov 26 '18 at 5:03
Are you importing "Realm" or "RealmSwift"?
– André
Nov 26 '18 at 11:55
I am importing RealmSwift
– Shararti KAKI
Nov 26 '18 at 12:39
Do you have any other List extensions? Is it in a separated file with RealmSwift imported? Did you try cleaning the project before building? Sorry to ask, but I remember seeing this error when I tried to implement this extension and it was solved under these conditions!
– André
Nov 28 '18 at 0:49
No I have not a single extension related to RealmSwift
– Shararti KAKI
Nov 28 '18 at 4:52
add a comment |
it gives me error "Extension of type 'List' with constraints cannot have an inheritance clause"
– Shararti KAKI
Nov 26 '18 at 5:03
Are you importing "Realm" or "RealmSwift"?
– André
Nov 26 '18 at 11:55
I am importing RealmSwift
– Shararti KAKI
Nov 26 '18 at 12:39
Do you have any other List extensions? Is it in a separated file with RealmSwift imported? Did you try cleaning the project before building? Sorry to ask, but I remember seeing this error when I tried to implement this extension and it was solved under these conditions!
– André
Nov 28 '18 at 0:49
No I have not a single extension related to RealmSwift
– Shararti KAKI
Nov 28 '18 at 4:52
it gives me error "Extension of type 'List' with constraints cannot have an inheritance clause"
– Shararti KAKI
Nov 26 '18 at 5:03
it gives me error "Extension of type 'List' with constraints cannot have an inheritance clause"
– Shararti KAKI
Nov 26 '18 at 5:03
Are you importing "Realm" or "RealmSwift"?
– André
Nov 26 '18 at 11:55
Are you importing "Realm" or "RealmSwift"?
– André
Nov 26 '18 at 11:55
I am importing RealmSwift
– Shararti KAKI
Nov 26 '18 at 12:39
I am importing RealmSwift
– Shararti KAKI
Nov 26 '18 at 12:39
Do you have any other List extensions? Is it in a separated file with RealmSwift imported? Did you try cleaning the project before building? Sorry to ask, but I remember seeing this error when I tried to implement this extension and it was solved under these conditions!
– André
Nov 28 '18 at 0:49
Do you have any other List extensions? Is it in a separated file with RealmSwift imported? Did you try cleaning the project before building? Sorry to ask, but I remember seeing this error when I tried to implement this extension and it was solved under these conditions!
– André
Nov 28 '18 at 0:49
No I have not a single extension related to RealmSwift
– Shararti KAKI
Nov 28 '18 at 4:52
No I have not a single extension related to RealmSwift
– Shararti KAKI
Nov 28 '18 at 4:52
add a comment |
You can use extensions for List
Swift 4.1
extension List: Decodable where Element: Decodable {
public convenience init(from decoder: Decoder) throws {
// Initialize self here so we can get type(of: self).
self.init()
assertTypeIsDecodable(Element.self, in: type(of: self))
let metaType = (Element.self as Decodable.Type) // swiftlint:disable:this force_cast
var container = try decoder.unkeyedContainer()
while !container.isAtEnd {
let element = try metaType.init(__from: &container)
self.append(element as! Element) // swiftlint:disable:this force_cast
}
}
}
extension List: Encodable where Element: Decodable {
public func encode(to encoder: Encoder) throws {
assertTypeIsEncodable(Element.self, in: type(of: self))
var container = encoder.unkeyedContainer()
for element in self {
// superEncoder appends an empty element and wraps an Encoder around it.
// This is normally appropriate for encoding super, but this is really what we want to do.
let subencoder = container.superEncoder()
try (element as! Encodable).encode(to: subencoder) // swiftlint:disable:this force_cast
}
}
}
it is error prone. first error it is giving on extension line is "Extension of type List with constraints can not have an inheritence clause"
– Shararti KAKI
Nov 19 '18 at 5:02
Above Code Snippet is for Swift 4+
– Sushil
Nov 22 '18 at 19:50
1
This uses conditional conformances which were added in Swift 4.1
– Sven
Nov 22 '18 at 20:44
Yes, Thank you Sven
– Sushil
Nov 22 '18 at 23:11
it gives me following errors 1> Extention of type 'List' with constraints cannot have an inheritance clause 2> Used of unresolved identifier 'assertTypeIsDecodable' 3> Incorrect argument label in call (have '__form; expected 'from:')
– Shararti KAKI
Nov 26 '18 at 5:46
|
show 2 more comments
You can use extensions for List
Swift 4.1
extension List: Decodable where Element: Decodable {
public convenience init(from decoder: Decoder) throws {
// Initialize self here so we can get type(of: self).
self.init()
assertTypeIsDecodable(Element.self, in: type(of: self))
let metaType = (Element.self as Decodable.Type) // swiftlint:disable:this force_cast
var container = try decoder.unkeyedContainer()
while !container.isAtEnd {
let element = try metaType.init(__from: &container)
self.append(element as! Element) // swiftlint:disable:this force_cast
}
}
}
extension List: Encodable where Element: Decodable {
public func encode(to encoder: Encoder) throws {
assertTypeIsEncodable(Element.self, in: type(of: self))
var container = encoder.unkeyedContainer()
for element in self {
// superEncoder appends an empty element and wraps an Encoder around it.
// This is normally appropriate for encoding super, but this is really what we want to do.
let subencoder = container.superEncoder()
try (element as! Encodable).encode(to: subencoder) // swiftlint:disable:this force_cast
}
}
}
it is error prone. first error it is giving on extension line is "Extension of type List with constraints can not have an inheritence clause"
– Shararti KAKI
Nov 19 '18 at 5:02
Above Code Snippet is for Swift 4+
– Sushil
Nov 22 '18 at 19:50
1
This uses conditional conformances which were added in Swift 4.1
– Sven
Nov 22 '18 at 20:44
Yes, Thank you Sven
– Sushil
Nov 22 '18 at 23:11
it gives me following errors 1> Extention of type 'List' with constraints cannot have an inheritance clause 2> Used of unresolved identifier 'assertTypeIsDecodable' 3> Incorrect argument label in call (have '__form; expected 'from:')
– Shararti KAKI
Nov 26 '18 at 5:46
|
show 2 more comments
You can use extensions for List
Swift 4.1
extension List: Decodable where Element: Decodable {
public convenience init(from decoder: Decoder) throws {
// Initialize self here so we can get type(of: self).
self.init()
assertTypeIsDecodable(Element.self, in: type(of: self))
let metaType = (Element.self as Decodable.Type) // swiftlint:disable:this force_cast
var container = try decoder.unkeyedContainer()
while !container.isAtEnd {
let element = try metaType.init(__from: &container)
self.append(element as! Element) // swiftlint:disable:this force_cast
}
}
}
extension List: Encodable where Element: Decodable {
public func encode(to encoder: Encoder) throws {
assertTypeIsEncodable(Element.self, in: type(of: self))
var container = encoder.unkeyedContainer()
for element in self {
// superEncoder appends an empty element and wraps an Encoder around it.
// This is normally appropriate for encoding super, but this is really what we want to do.
let subencoder = container.superEncoder()
try (element as! Encodable).encode(to: subencoder) // swiftlint:disable:this force_cast
}
}
}
You can use extensions for List
Swift 4.1
extension List: Decodable where Element: Decodable {
public convenience init(from decoder: Decoder) throws {
// Initialize self here so we can get type(of: self).
self.init()
assertTypeIsDecodable(Element.self, in: type(of: self))
let metaType = (Element.self as Decodable.Type) // swiftlint:disable:this force_cast
var container = try decoder.unkeyedContainer()
while !container.isAtEnd {
let element = try metaType.init(__from: &container)
self.append(element as! Element) // swiftlint:disable:this force_cast
}
}
}
extension List: Encodable where Element: Decodable {
public func encode(to encoder: Encoder) throws {
assertTypeIsEncodable(Element.self, in: type(of: self))
var container = encoder.unkeyedContainer()
for element in self {
// superEncoder appends an empty element and wraps an Encoder around it.
// This is normally appropriate for encoding super, but this is really what we want to do.
let subencoder = container.superEncoder()
try (element as! Encodable).encode(to: subencoder) // swiftlint:disable:this force_cast
}
}
}
edited Nov 22 '18 at 20:41
Sven
20.4k44568
20.4k44568
answered Nov 16 '18 at 23:47
SushilSushil
12018
12018
it is error prone. first error it is giving on extension line is "Extension of type List with constraints can not have an inheritence clause"
– Shararti KAKI
Nov 19 '18 at 5:02
Above Code Snippet is for Swift 4+
– Sushil
Nov 22 '18 at 19:50
1
This uses conditional conformances which were added in Swift 4.1
– Sven
Nov 22 '18 at 20:44
Yes, Thank you Sven
– Sushil
Nov 22 '18 at 23:11
it gives me following errors 1> Extention of type 'List' with constraints cannot have an inheritance clause 2> Used of unresolved identifier 'assertTypeIsDecodable' 3> Incorrect argument label in call (have '__form; expected 'from:')
– Shararti KAKI
Nov 26 '18 at 5:46
|
show 2 more comments
it is error prone. first error it is giving on extension line is "Extension of type List with constraints can not have an inheritence clause"
– Shararti KAKI
Nov 19 '18 at 5:02
Above Code Snippet is for Swift 4+
– Sushil
Nov 22 '18 at 19:50
1
This uses conditional conformances which were added in Swift 4.1
– Sven
Nov 22 '18 at 20:44
Yes, Thank you Sven
– Sushil
Nov 22 '18 at 23:11
it gives me following errors 1> Extention of type 'List' with constraints cannot have an inheritance clause 2> Used of unresolved identifier 'assertTypeIsDecodable' 3> Incorrect argument label in call (have '__form; expected 'from:')
– Shararti KAKI
Nov 26 '18 at 5:46
it is error prone. first error it is giving on extension line is "Extension of type List with constraints can not have an inheritence clause"
– Shararti KAKI
Nov 19 '18 at 5:02
it is error prone. first error it is giving on extension line is "Extension of type List with constraints can not have an inheritence clause"
– Shararti KAKI
Nov 19 '18 at 5:02
Above Code Snippet is for Swift 4+
– Sushil
Nov 22 '18 at 19:50
Above Code Snippet is for Swift 4+
– Sushil
Nov 22 '18 at 19:50
1
1
This uses conditional conformances which were added in Swift 4.1
– Sven
Nov 22 '18 at 20:44
This uses conditional conformances which were added in Swift 4.1
– Sven
Nov 22 '18 at 20:44
Yes, Thank you Sven
– Sushil
Nov 22 '18 at 23:11
Yes, Thank you Sven
– Sushil
Nov 22 '18 at 23:11
it gives me following errors 1> Extention of type 'List' with constraints cannot have an inheritance clause 2> Used of unresolved identifier 'assertTypeIsDecodable' 3> Incorrect argument label in call (have '__form; expected 'from:')
– Shararti KAKI
Nov 26 '18 at 5:46
it gives me following errors 1> Extention of type 'List' with constraints cannot have an inheritance clause 2> Used of unresolved identifier 'assertTypeIsDecodable' 3> Incorrect argument label in call (have '__form; expected 'from:')
– Shararti KAKI
Nov 26 '18 at 5:46
|
show 2 more comments
Had the same problem in a project and wrote these extensions:
import Foundation
import RealmSwift
extension RealmSwift.List: Decodable where Element: Decodable {
public convenience init(from decoder: Decoder) throws {
self.init()
let container = try decoder.singleValueContainer()
let decodedElements = try container.decode([Element].self)
self.append(objectsIn: decodedElements)
}
}
extension RealmSwift.List: Encodable where Element: Encodable {
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(self.map { $0 })
}
}
With these extension you easily can make the Realm Object Codable. Like this
@objcMembers public class MyModel: Object, Codable {
dynamic var id: String = ""
dynamic var name: String = ""
var favorites = List<String>()
var subjects = List<UserSubject>()
}
@objcMembers public class UserSubject: Object, Codable {
dynamic var id: String = ""
dynamic var name: String = ""
var teachers = List<Publications>()
}
@objcMembers public class Publications: Object, Codable {
dynamic var id: String = ""
dynamic var name: String = ""
var editors = List<Editor>()
}
@objcMembers public class Editor: Object, Codable {
}
it gives me error "Extension of type 'List' with constraints cannot have an inheritance clause"
– Shararti KAKI
Nov 26 '18 at 5:44
Properties which are aList<T>
do not have to be dynamic. Did you make all elements implement theCodable
protocol?
– Oliver
Nov 28 '18 at 8:15
I do not know how to implement it that is main problem
– Shararti KAKI
Nov 28 '18 at 13:30
I updated my answer which is now containing your classes with theCodable
protocol. Actually i's very easy: When you make a classCodable
all it members have to beCodable
as well.
– Oliver
Nov 28 '18 at 17:35
add a comment |
Had the same problem in a project and wrote these extensions:
import Foundation
import RealmSwift
extension RealmSwift.List: Decodable where Element: Decodable {
public convenience init(from decoder: Decoder) throws {
self.init()
let container = try decoder.singleValueContainer()
let decodedElements = try container.decode([Element].self)
self.append(objectsIn: decodedElements)
}
}
extension RealmSwift.List: Encodable where Element: Encodable {
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(self.map { $0 })
}
}
With these extension you easily can make the Realm Object Codable. Like this
@objcMembers public class MyModel: Object, Codable {
dynamic var id: String = ""
dynamic var name: String = ""
var favorites = List<String>()
var subjects = List<UserSubject>()
}
@objcMembers public class UserSubject: Object, Codable {
dynamic var id: String = ""
dynamic var name: String = ""
var teachers = List<Publications>()
}
@objcMembers public class Publications: Object, Codable {
dynamic var id: String = ""
dynamic var name: String = ""
var editors = List<Editor>()
}
@objcMembers public class Editor: Object, Codable {
}
it gives me error "Extension of type 'List' with constraints cannot have an inheritance clause"
– Shararti KAKI
Nov 26 '18 at 5:44
Properties which are aList<T>
do not have to be dynamic. Did you make all elements implement theCodable
protocol?
– Oliver
Nov 28 '18 at 8:15
I do not know how to implement it that is main problem
– Shararti KAKI
Nov 28 '18 at 13:30
I updated my answer which is now containing your classes with theCodable
protocol. Actually i's very easy: When you make a classCodable
all it members have to beCodable
as well.
– Oliver
Nov 28 '18 at 17:35
add a comment |
Had the same problem in a project and wrote these extensions:
import Foundation
import RealmSwift
extension RealmSwift.List: Decodable where Element: Decodable {
public convenience init(from decoder: Decoder) throws {
self.init()
let container = try decoder.singleValueContainer()
let decodedElements = try container.decode([Element].self)
self.append(objectsIn: decodedElements)
}
}
extension RealmSwift.List: Encodable where Element: Encodable {
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(self.map { $0 })
}
}
With these extension you easily can make the Realm Object Codable. Like this
@objcMembers public class MyModel: Object, Codable {
dynamic var id: String = ""
dynamic var name: String = ""
var favorites = List<String>()
var subjects = List<UserSubject>()
}
@objcMembers public class UserSubject: Object, Codable {
dynamic var id: String = ""
dynamic var name: String = ""
var teachers = List<Publications>()
}
@objcMembers public class Publications: Object, Codable {
dynamic var id: String = ""
dynamic var name: String = ""
var editors = List<Editor>()
}
@objcMembers public class Editor: Object, Codable {
}
Had the same problem in a project and wrote these extensions:
import Foundation
import RealmSwift
extension RealmSwift.List: Decodable where Element: Decodable {
public convenience init(from decoder: Decoder) throws {
self.init()
let container = try decoder.singleValueContainer()
let decodedElements = try container.decode([Element].self)
self.append(objectsIn: decodedElements)
}
}
extension RealmSwift.List: Encodable where Element: Encodable {
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(self.map { $0 })
}
}
With these extension you easily can make the Realm Object Codable. Like this
@objcMembers public class MyModel: Object, Codable {
dynamic var id: String = ""
dynamic var name: String = ""
var favorites = List<String>()
var subjects = List<UserSubject>()
}
@objcMembers public class UserSubject: Object, Codable {
dynamic var id: String = ""
dynamic var name: String = ""
var teachers = List<Publications>()
}
@objcMembers public class Publications: Object, Codable {
dynamic var id: String = ""
dynamic var name: String = ""
var editors = List<Editor>()
}
@objcMembers public class Editor: Object, Codable {
}
edited Nov 28 '18 at 8:20
answered Nov 23 '18 at 14:08
OliverOliver
9711
9711
it gives me error "Extension of type 'List' with constraints cannot have an inheritance clause"
– Shararti KAKI
Nov 26 '18 at 5:44
Properties which are aList<T>
do not have to be dynamic. Did you make all elements implement theCodable
protocol?
– Oliver
Nov 28 '18 at 8:15
I do not know how to implement it that is main problem
– Shararti KAKI
Nov 28 '18 at 13:30
I updated my answer which is now containing your classes with theCodable
protocol. Actually i's very easy: When you make a classCodable
all it members have to beCodable
as well.
– Oliver
Nov 28 '18 at 17:35
add a comment |
it gives me error "Extension of type 'List' with constraints cannot have an inheritance clause"
– Shararti KAKI
Nov 26 '18 at 5:44
Properties which are aList<T>
do not have to be dynamic. Did you make all elements implement theCodable
protocol?
– Oliver
Nov 28 '18 at 8:15
I do not know how to implement it that is main problem
– Shararti KAKI
Nov 28 '18 at 13:30
I updated my answer which is now containing your classes with theCodable
protocol. Actually i's very easy: When you make a classCodable
all it members have to beCodable
as well.
– Oliver
Nov 28 '18 at 17:35
it gives me error "Extension of type 'List' with constraints cannot have an inheritance clause"
– Shararti KAKI
Nov 26 '18 at 5:44
it gives me error "Extension of type 'List' with constraints cannot have an inheritance clause"
– Shararti KAKI
Nov 26 '18 at 5:44
Properties which are a
List<T>
do not have to be dynamic. Did you make all elements implement the Codable
protocol?– Oliver
Nov 28 '18 at 8:15
Properties which are a
List<T>
do not have to be dynamic. Did you make all elements implement the Codable
protocol?– Oliver
Nov 28 '18 at 8:15
I do not know how to implement it that is main problem
– Shararti KAKI
Nov 28 '18 at 13:30
I do not know how to implement it that is main problem
– Shararti KAKI
Nov 28 '18 at 13:30
I updated my answer which is now containing your classes with the
Codable
protocol. Actually i's very easy: When you make a class Codable
all it members have to be Codable
as well.– Oliver
Nov 28 '18 at 17:35
I updated my answer which is now containing your classes with the
Codable
protocol. Actually i's very easy: When you make a class Codable
all it members have to be Codable
as well.– Oliver
Nov 28 '18 at 17:35
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2f53332732%2fhow-to-implement-codable-while-using-realm%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