gonzalezreal / defaultcodable Goto Github PK
View Code? Open in Web Editor NEWA convenient way to handle default values with Swift Codable types
License: MIT License
A convenient way to handle default values with Swift Codable types
License: MIT License
So if I have to define a default value of 1.0, I can implement something like this:
public enum OneDouble: DefaultValueProvider {
public static let `default`: Double = 1.0
}
And use it like this:
public class Estimate: Codable {
@Default<OneDouble>
public var fees: Double
}
For some reasons if I decide to change the value to 0.5, then I have to change it or add another enum:
public enum PointFiveDouble: DefaultValueProvider {
public static let `default`: Double = 0.5
}
This looks cumbersome to me.
Is there any way to just use it like this:
public class Estimate: Codable {
@Default(0.5)
public var fees: Double
}
Hi Guille
I love your open source projects, many of which are related to my work. Thank you for your work.
I'm working on a job implementing serialization and deserialization of NSFont and UIFont, aka Codable.
My code is like this:
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
if let font = self.wrappedValue{
let descriptorData = try? NSKeyedArchiver.archivedData(withRootObject: font.fontDescriptor, requiringSecureCoding: false)
try container.encode(descriptorData, forKey: .descriptor)
try container.encode(font.pointSize, forKey: .pointSize)
try container.encode(font.fontName, forKey: .fontName)
}
}
After I saw your open source project, I really liked this way of writing.
So I would like to give a suggestion, is there any Codable that supports more types. For example: the relevant support type of NSKeyedUnarchiver.
Also, after this type of parsing fails, a default value is returned.
Just an idea, if you have the same idea, we might as well give it a try.
Would be possible to add Carthage support for the library?
Issue when building on Simulator on Apple Silicon.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.