Swiftpack.co - cicout/cico_auto_codable as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by cicout.
cicout/cico_auto_codable 0.30.39
AutoCodable
⭐️ 3
🕓 2 years ago
iOS
.package(url: "https://github.com/cicout/cico_auto_codable.git", from: "0.30.39")

CICOAutoCodable

Swift5 compatible Carthage compatible CocoaPods License MIT

CICOAutoCodable is an extension for codable, a new feature in Swift 4. It is very simple to achieve mutual conversion between model and JSON. And it provides automatic code completion using sourcery.

Installation

You can simply add CICOAutoCodable to your Cartfile:

github "cicout/cico_auto_codable"

Sample Code

Model And JSON Definition

enum MyEnum: String, CICOAutoCodable {
    case one
    case two
}

class MyClass: CICOAutoCodable {
    private(set) var stringValue: String?
    private(set) var dateValue: Date?
    private(set) var intValue: Int = 0
    private(set) var doubleValue: Double = 1.0
    private(set) var boolValue: Bool = false
    private(set) var enumValue: MyEnum = .one
    private(set) var urlValue: URL?
    private(set) var nextValue: MyClass?
    private(set) var arrayValue: [String]?
    private(set) var dicValue: [String: String]?
}
{
    "stringValue": "string",
    "dateValue": 1234567890123,
    "intValue": 123,
    "doubleValue": 2.5,
    "boolValue": true,
    "enumValue": "two",
    "urlValue": "https://www.google.com",
    "nextValue": {
        "stringValue": "string",
        "intValue": 123,
        "doubleValue": 2.5,
        "boolValue": true,
        "enumValue": "two"
    },
    "arrayValue": [
              "string0",
              "string1",
              ],
    "dicValue": {
        "key0": "value0",
        "key1": "value1"
    }
}

JSON TO Model

  • Default JSONDecoder
let object = MyClass.init(jsonString: myJSONString)
  • Custom JSONDecoder
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .millisecondsSince1970
let object = MyClass.init(jsonString: myJSONString, jsonDecoder: decoder)

Model TO JSON

  • Default JSONEncoder
let jsonString = object?.toJSONString()
  • Custom JSONEncoder
let encoder = JSONEncoder()
encoder.dateEncodingStrategy = .millisecondsSince1970
let jsonString = object?.toJSONString(jsonEncoder: encoder)

OBJ-C Wrapper

  • OCCodingObjectWrapper
    OCCodingObjectWrapper can make NSCoding Class in OBJ-C conform to Codable protocol in Swift.
@interface OCTestClass : NSObject <NSCoding>

@property (nonatomic, strong) NSString *text;

@end

@implementation OCTestClass

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super init];
    if (self) {
        self.text = [coder decodeObjectForKey:@"text"];
    }
    return self;
}

- (void)encodeWithCoder:(NSCoder *)coder
{
    [coder encodeObject:self.text forKey:@"text"];
}

@end
class SwiftTestClass: CICOAutoCodable {
    var objectValue: OCCodingObjectWrapper<OCTestClass>?
}
  • OCEnumWrapper
    OCEnumWrapper can make integer enum in OBJ-C conform to Codable protocol in Swift.
NS_ENUM(NSInteger, OCTestIntEnum) {
    one = 1,
    two
};
class SwiftTestClass: CICOAutoCodable {
    var enumValue: OCEnumWrapper<OCTestIntEnum>?
}

Auto Code Completion

You don't need to write any mapping code when there is no custom mapping relationship using codable. However, you need to manually define the CodingKeys enumeration and list all the mappings, including the part that does not require a custom mapping, when there is any custom mapping relationship. CICOAutoCodable can complete the code for you automaticaly using sourcery.

About Sourcery

Install Sourcery

  1. Copy "sourcery" directory in this framework source into your project;
  2. Get the latest sourcery by CocoaPod; (replace {YourProjectDir} with your real project directory)
cd "{YourProjectDir}"/sourcery/source
pod update
  1. Open "yourProjectTarget" -> "Build Phases" -> "+" -> "New Run Script Phase", and add new run script below: (replace {YourProjectName} with your real project name)
if [ "${CONFIGURATION}" = "Debug" ]; then
echo "[***** Start Running CICOAutoCodable Script *****]"
./sourcery/source/Pods/Sourcery/bin/sourcery --sources ./Carthage/Checkouts/cico_auto_codable/CICOAutoCodable --sources ./"{YourProjectName}" --templates ./Carthage/Checkouts/cico_auto_codable/sourcery/templates/ --output ./sourcery/auto_generated
echo "[***** End Running CICOAutoCodable Script *****]"
fi
  1. Define your model and run;

Sample Code

Model Definition

enum MyEnum: String, CICOAutoCodable {
    case one
    case two
}

class MyClass: CICOAutoCodable {
    private(set) var stringValue: String?
    private(set) var dateValue: Date?
    private(set) var intValue: Int = 0
    private(set) var doubleValue: Double = 1.0
    private(set) var boolValue: Bool = false
    private(set) var enumValue: MyEnum = .one
    private(set) var urlValue: URL?
    private(set) var next: MyClass?
    private(set) var arrayValue: [String]?
    private(set) var dicValue: [String: String]?
    private(set) var ignoredValue: String?
}

Using Custom And Ignored CodingKeys

  • CodingKeys Definition
extension MyClass {
    enum CICOCustomCodingKeys: String {
        case next = "nextValue"
    }
    
    enum CICOIgnoredCodingKeys: String {
        case ignoredValue
    }
}
  • Auto Generated Code
// sourcery:inline:auto:MyClass.CICOAutoCodable_Auto_Generated_CodingKeys_Head
    enum CodingKeys: String, CodingKey {
// sourcery:inline:auto:MyClass.CodingKeys.CICOAutoCodable_Auto_Generated_Custom_CodingKeys
        case next = "nextValue"
// sourcery:inline:auto:MyClass.CodingKeys.CICOAutoCodable_Auto_Generated_CodingKeys
        case stringValue
        case dateValue
        case intValue
        case doubleValue
        case boolValue
        case enumValue
        case urlValue
        case arrayValue
        case dicValue
// sourcery:inline:auto:MyClass..CICOAutoCodable_Auto_Generated_CodingKeys_Tail
    }
// sourcery:end

Using CodingKeys Directly

  • CodingKeys Definition
extension MyClass {
    enum CICOIgnoredCodingKeys: String {
        case ignoredValue
    }
    
    enum CodingKeys: String, CodingKey {
        case next = "nextValue"
    }
}
  • Auto Generated Code
extension MyClass {
    enum CICOIgnoredCodingKeys: String {
        case ignoredValue
    }
    
    enum CodingKeys: String, CodingKey {
        case next = "nextValue"

// sourcery:inline:auto:MyClass.CodingKeys.CICOAutoCodable_Auto_Generated_CodingKeys
        case stringValue
        case dateValue
        case intValue
        case doubleValue
        case boolValue
        case enumValue
        case urlValue
        case arrayValue
        case dicValue
// sourcery:end
    }
}

Requirements

  • iOS 8.0+
  • Swift 4.0+

License

CICOAutoCodable is released under the MIT license. See LICENSE for details.

More

Have a question? Please open an issue!

GitHub

link
Stars: 3
Last commit: 2 years ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics