ValueCoding

ValueCoding is a powerful and easy-to-use Swift framework that provides support for value types conforming to the NSCoding protocol. It simplifies the process of encoding and decoding custom value types, allowing developers to focus on their application logic rather than dealing with serialization and deserialization.

Features

  • Easy integration with value types
  • Automatic encoding and decoding of values
  • Supports custom serialization and deserialization
  • Handles complex value types and nested structures
  • Supports value type versioning
  • Compatible with iOS, macOS, watchOS, and tvOS

Installation

You can easily integrate ValueCoding into your project using CocoaPods. Simply add the following line to your Podfile:

// Podfile

pod 'ValueCoding'

Usage

To start using ValueCoding, follow these simple steps:

Step 1: Conform to ValueCoding

In your custom value type, conform to the ValueCoding protocol:

// MyCustomValueType.swift

import ValueCoding

struct MyCustomValueType: ValueCoding {
    let intValue: Int
    let stringValue: String

    // Implement required static properties
    static var codingValueType: MyCustomValueType.Type { MyCustomValueType.self }
    static var uid: String { "com.yourdomain.MyCustomValueType" }
}

Step 2: Encode and Decode

To encode an instance of your custom value type into data:

// Encoding

let myValue = MyCustomValueType(intValue: 42, stringValue: "Hello, World!")
let encodedData = try myValue.encode()

To decode an instance from data:

// Decoding

let decodedValue = try MyCustomValueType.decode(from: encodedData)

Custom Serialization and Deserialization

In some cases, you may need to customize the serialization and deserialization process. To do so, simply implement the optional methods in the ValueCoding protocol:

// MyCustomValueType.swift

import ValueCoding

struct MyCustomValueType: ValueCoding {
    let intValue: Int
    let stringValue: String

    static var codingValueType: MyCustomValueType.Type { MyCustomValueType.self }
    static var uid: String { "com.yourdomain.MyCustomValueType" }

    // Optional: Custom encoding method
    func encodeValue() throws -> Data {
        // Custom encoding logic
        // Return encoded data
    }

    // Optional: Custom decoding method
    static func decodeValue(from data: Data) throws -> MyCustomValueType {
        // Custom decoding logic
        // Return decoded value
    }
}

More Information

For more detailed information including advanced usage, handling versioning, and integration examples, refer to the official ValueCoding GitHub repository.

ValueCoding empowers Swift developers to effortlessly serialize and deserialize custom value types. With its simple integration and powerful features, it is the ideal framework for working with value types conforming to the NSCoding protocol.