How to turn a standard model class/struct into observable properties? - swift

I am using ReactiveKit with their Bond extension, and I can't really figure out how to do something that feels kind of basic.
Let's say I have a User model in my app. Something like this.
class User: Codable {
var id: String
var firstName: String?
var avatar: String?
}
The content comes from a remote API, by making it confirm to Codable everything works nice and easy.
But let's also say for example that I would like a bidirectional binding from a model property to some UI state; that is not possible since none of my properties confirm to the BindableProtocol. Or if I want to observe changes to my model's properties, this is not possible either of course.
So my question is: how do I turn my model properties in actual observable Properties, without breaking the existing User model and behavior? For example, it still needs to be Codable. Do I make a second ObservableUser class, and then have didSet in all properties on the User model to write changes to the ObservableUser? And something similar on the ObservableUser to write changes back to the User? That seems horrible and hopefully not the recommenced way forward.

You don't need to create another object to make model properties observable. But there is definitely an overhead of implementing the decoder & encoder methods and coding keys enum as below which is also a standard practice in many cases,
class User: Codable {
var id: String = ""
var firstName = Observable<String?>(nil)
var avatar = Observable<String?>(nil)
private enum CodingKeys: String, CodingKey {
case id, firstName, avatar
}
required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.id = try container.decode(String.self, forKey: .id)
self.firstName.value = try container.decode(String.self, forKey: .firstName)
self.avatar.value = try container.decode(String.self, forKey: .avatar)
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(self.id, forKey: .id)
try container.encode(self.firstName.value, forKey: .firstName)
try container.encode(self.avatar.value, forKey: .avatar)
}
}
Now you are able to bind any UIView element.
Normally, with reactive approach, ViewModel is where you create bindable properties that provide value from your model to view and update model property by keeping the model invisible by the view.

Related

Trying to make a class codable in Swift but it is not the correct format?

I am creating a class that conforms to codable.
I have this:
import Foundation
class Attribute : Decodable {
var number: Int16
var label: String?
var comments: String?
init(number:Int16, label:String?, comments:String?) {
self.number = number
self.label = label
self.comments = comments
}
// Everything from here on is generated for you by the compiler
required init(from decoder: Decoder) throws {
let keyedContainer = try decoder.container(keyedBy: CodingKeys.self)
number = try keyedContainer.decode(Int16.self, forKey: .number)
label = try keyedContainer.decode(String.self, forKey: .label)
comments = try keyedContainer.decode(String.self, forKey: .comments)
}
enum CodingKeys: String, CodingKey {
case number
case label
case comments
}
}
extension Attribute: Encodable {
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(number, forKey: .number)
try container.encode(label, forKey: .label)
try container.encode(comments, forKey: .comments)
}
}
This is apparently fine.
I create an instance of Attribute and encode it using:
let newAttribute = Attribute.init(number:value.number, label:value.label, comments:value.shortcut)
Then I create an array of these attributes and encode that array using
let array = try JSONEncoder().encode(array)
This will encode the array of Attribute to Data.
Then I try to convert the Data object back to the array of Attribute using this:
let array = try JSONDecoder().decode(Attribute.self, from: data) as! Array<Attribute>
First error I get is this:
Cast from 'Attribute' to unrelated type 'Array< Attribute>' always fails
If I remove the cast part I catch this error when the decode tries...
Optional("The data isn’t in the correct format.")
Any ideas?
You need to pass in the array to decode, don't pass in the array element type, then try to force-cast that to an array, that doesn't make any sense. YourType and Array<YourType> are two different and completely unrelated types, so you cannot cast one to the other and you need to use the specific type when calling JSONDecoder.decode(_:from:).
let array = try JSONDecoder().decode([Attribute].self, from: data)
Btw as already pointed out in your previous question, there is no need to manually write the init(from:) and encode(to:) methods or the CodingKeys enum since for your simple type, the compiler can auto-synthesise all of those for you. Also, if you used a struct instead of class, you'd also get the member wise initialiser for free.

Is it possible to use Codable subclasses without explicitly decoding?

I've used struct MyThing:Codable { } to parse JSON a few times. Now I'd like to use a class instead of struct, because struct isn't really suited for the kind of work I need done (mutability, conformance etc.), and some subclassing. I tried this:
class Beverage:Codable{
var name:String = ""
}
class Beer:Beverage{
var alcoholPercent:Double = 0
}
I don't necessarily receive a list of multiple different types of beverages in the same list, let's say I just want to decode a list of [Beer]. If I try this with a json with a single beer like {"name": "Hansa", "alcoholPercent": 5.4} and try to decode it to the Beer-class, like this:
let beer = try! JSONDecoder().decode(Beer.self, from: json.data(using: .utf8)!)
the beer will have name: Hansa and alcoholPercent: 0.0. The name is correct, but the alcoholPercent is the default value in the class.
Is there a magic way to make subclasses automatically conform to Codable without explicitly setting the key/value of every variable?
This is working:
class Beverage:Codable{
var name:String = ""
}
class Beer:Beverage{
var alcoholPercent:Double = 0
enum CodingKeys:String, CodingKey{
case alcoholPercent
}
required init(from decoder: Decoder) throws {
let values = try! decoder.container(keyedBy: CodingKeys.self)
self.alcoholPercent = try! values.decode(Double.self, forKey: .alcoholPercent)
try super.init(from: decoder)
}
}
With this code, I get "Hansa" and 5.4.
Why do I have to explicitly conform to Codable for subclasses, but not for the base class for this to work? Is there a way to do this without all the manual code? I feel like this should've worked out-of-the-box without needing the required init.
What you are asking for is compiler synthesis of an override of init(from:) in Beer. Swift doesn't do that. This has been discussed on the Swift forum, for example in this thread. Itai Ferber is the primary Apple engineer responsible for the design and implementation of Codable so you can consider his response authoritative.
If you remove the Codable conformance from Beverage and add it to Beer directly (and to any other leaf subclasses of Beverage) then you might get the behavior you want. That will only work if you don't need the conformance on Beverage itself though.

Swift Codable protocol with Strings and UIImages

I am trying to save data via a codable protocol. It is an array of structures with structs inside. There are strings, images and bool values and I am thinking it is one of these data types that does not conform to the protocol.
Here is a pic of all the data I must save:
original
struct Checklist {
var name: String
var image: UIImage?
var items: [Item] = []
}
struct Item {
var nameOfNote: String
var remind: Bool
//var date: Date
}
struct alldata: Codable {
var checklists: [Checklist] = []
}
I have tried to include the protocol in all the structs but it also produced an error. Here's a pic:
tried a solution picture
JSON in Swift 4 is fun (repeat this at 2am in the morning ;)).
One of the first things I always do is consult a good blog, like Ultimate Guide to JSON Parsing with Swift 4. While it might not answer the "direct" question, it will provide a lot of useful information.
Another thing to do, is consult the Apple documentation. In this, a quick look at the documentation for UIImage will point out that it does not conform to Codable, so that's a problem.
Another issue is, JSON doesn't support binary data, it's a text based solution.
This means that you need to figure out away to convert the binary image data to text. Lucky for us, people have already thought about this and the most common mechanism is to use Base 64 encoding.
A quick google search will turn up any number of solutions for encoding/decoding a UIImage to/from base 64, for example Convert between UIImage and Base64 string
Now, we just need to customise the encoding and decoding process.
The first thing is to make sure all the other fields/objects/data we use are also conform to Codable...
struct Item: Codable {
var nameOfNote: String
var remind: Bool
//var date: Date
}
Then we can set out customising the encoding and decoding process for the image, maybe something like...
struct Checklist: Codable {
enum CodingKeys: String, CodingKey {
case image
case name
case items
}
var name: String
var image: UIImage?
var items: [Item] = []
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
name = try container.decode(String.self, forKey: .name)
items = try container.decode([Item].self, forKey: .items)
if let text = try container.decodeIfPresent(String.self, forKey: .image) {
if let data = Data(base64Encoded: text) {
image = UIImage(data: data)
}
}
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
if let image = image, let data = image.pngData() {
try container.encode(data, forKey: .image)
}
try container.encode(name, forKey: .name)
try container.encode(items, forKey: .items)
}
}
nb: This is the basic process I've used for a couple of projects, I've not tested the above myself, but it should present the basic idea you need to get started

How to use Codable and Coredata together?

I am trying to implement Codable with Coredata. I've tried following the following answer, but still have had no luck.
How to use swift 4 Codable in Core Data?
The error/problem I am having is my project is continuing to say: "Argument type 'User' does not conform to expected type 'Encodable' whenever I try to encode or decode the object.
I have created the Entity in CoreData and made NSManagedObject subclasses:
import Foundation
import CoreData
#objc(User)
public class User: NSManagedObject, Codable {
// MARK: - Codable setUp
enum CodingKeys: String, CodingKey {
case fullname
case email
case zipcode
case usertype = "user_type"
}
// MARK: - Decoding the data
required convenience init(from decoder: Decoder) {
guard let context = decoder.userInfo[.context] as? NSManagedObjectContext else {NSLog("Error: with User context!")
return
}
guard let entity = NSEntityDescription.entity(forEntityName: "User", in: context) else {
NSLog("Error with user enity!")
return
}
self.init(entity: entity, in: context)
let values = try decoder.container(keyedBy: CodingKeys.self)
fullname = try values.decode(String.self, forkey: .fullname)
email = try values.decode(String.self, forkey: .email)
zipcode = try values.decode(String.self, forkey: .zipcode)
userType = try values.decode(String.self, forkey: .userType)
}
// MARK: - Encoding the data
func encode(to encoder: Encoder) throws {
var container = try encoder.container(keyedBy: CodingKeys.self)
try container.encode(fullname, forkey: .fullname)
try container.encode(email, forkey: .email)
try container.encode(usertype, forkey: .usertype)
try container.encode(zipcode, forkey: .zipcode)
}
}
// This helps with decoding
extension CodingUserInfoKey {
static let context = CodingUserInfoKey(rawValue: "context")
}
When I try to decode the object and save the user to firebase I get a warning that says "In argument type 'User.Type', 'User' does not conform to expected type 'Decodable'
When I try to encode I get a warning that says "Argument type 'User' does not conform to expected type 'Encodable'
User' does not conform to expected type 'Decodable
occurs due to a typo (case sensitivity matters):
case usertype ... vs. ...forKey: .userType)
and the encode/decode(:forKey:) methods have a capital K
Your Problem might not be related to CoreData/Codable, but rather to an imported User model, that in fact does not conform to Codable.
Firebase defines a User in their FirebaseAuth.framework (Reference). If you import Firebase at the top of your file, it will also import that User declaration. IMO this is bad API design by Firebase, because many apps that also use FirebaseAuth will have a User model with some custom properties at some point...
If you don't need FirebaseAuth in that file, just import the more specific Firebase framework, e.g. import FirebaseDatabase to avoid importing their User model.
Otherwise, to help the compiler selecting your User model instead of the imported model, you can explicitly prepend User.self with your module name: YourModule.User.self.

How can I decode when I don't know the type, with class inheritance?

I have a base class Action, which is an Operation. It has a bunch of crufty Operation stuff in it (KVO and all that). The base class itself doesn't actually need to encode/decode anything.
class Action : Operation, Codable {
var _executing = false
...
}
I have a bunch of Action sub-classes, like DropboxUploadAction, which are directly instantiated with an Input struct they define:
let actionInput = DropboxUploadAction.Input.init(...)
ActionManager.shared.run(DropboxUploadAction.init(actionInput, data: binaryData), completionBlock: nil)
Here's what the subclasses look like:
class DropboxUploadAction : Action {
struct Input : Codable {
var guid: String
var eventName: String
var fileURL: URL?
var filenameOnDropbox: String
var share: Bool
}
struct Output : Codable {
var sharedFileLink: String?
var dropboxPath: String?
}
var input: Input
var output: Output
...
required init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
input = try values.decode(Input.self, forKey: .input)
output = try values.decode(Output.self, forKey: .output)
let superDecoder = try values.superDecoder()
try super.init(from: superDecoder)
}
fileprivate enum CodingKeys: String, CodingKey {
case input
case output
}
override func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(input, forKey: .input)
try container.encode(output, forKey: .output)
try super.encode(to: container.superEncoder())
}
}
When some situations occur such as a loss of internet connectivity, these classes need to be serialized to disk for later. That's fine, because at the time I have references to them and can encode them with JSONEncoder().encode(action), no problem.
But later when I want to deserialize them, I need to specify the type of the class and I don't know what it is. I have some data and I know it can be decoded to a class that inherits from Action, but I don't know which subclass it is. I'm loathe to encode that in the filename. Is there some way to decode it as the base class Action, then in the decode() method of Action, somehow detect the proper class and redirect?
In the past I've used NSKeyedUnarchiver.setClass() to handle this. But I don't know how to do that with Swift 4's Codable, and I understand that NSCoding is deprecated now so I shouldn't use NSKeyedUnarchiver anymore...
If it helps: I have a struct Types : OptionSet, Codable which each subclass returns, so I don't have to use the name of the class as its identity.
Thanks for any help!
Uhhh NSCoding isn't deprecated. We still use it when instantiating UIViewControllers from storyboard via init(coder:).
Also, if you still don't want to use NSCoding, you can just store the Input, Output and Types to a struct and serialize that to disk instead.
struct SerializedAction {
let input: Input
let output: Output
let type: Type
}
When needed, you can decode that and decide the correct Action to initialize with your input/output via the type property.
class DropboxAction: Action {
...
init(input: Input, output: Output) {
...
}
}
You don't necessarily need to encode the entire Action object.