Key-Value Coding (KVC) for native Swift classes and structs

Related tags

Reflection SwiftKVC
Overview

SwiftKVC

SwiftKVC brings key-value coding to native Swift classes and structures. You can easily set and access properties just using a subscript:

var person = Person()
person["name"] = "John"

Or use the more verbose method to catch potential errors:

var person = Person()
do {
  try person.set(value: "John", key: "name")
} catch {
  print(error)
}

SwiftKVC brings the power of Cocoa style key-value coding to Swift.

Installation

SwiftKVC is available through CocoaPods. To install, simply include the following lines in your podfile:

use_frameworks!
pod 'SwiftKVC'

Be sure to import the module at the top of your .swift files:

import SwiftKVC

Alternatively, clone this repo or download it as a zip and include the classes in your project.

Usage

To enable key-value coding for a native Swift structure or class, simply have it conform to either Value or Object respectively:

struct Person : Value {
  var name: String
  var age: Int
}

You can then set and retrieve values from your model by key:

person["name"] = "John"
person["age"] = 36
if let id = person["id"] as? Int {
  print(id)
}

If you would like to handle possible errors, you can use the more verbose methods:

do {
  try person.set(value: "John", key: "name")
  try person.set(value: 36, key: "age")
  if let id = try person.get(key: "id") as? Int {
    print(id)
  }
} catch {
  print(error)
}

Author

Brad Hilton, [email protected]

License

SwiftKVC is available under the MIT license. See the LICENSE file for more info.

You might also like...
An Objective-C wrapper for RocksDB - A Persistent Key-Value Store for Flash and RAM Storage.

ObjectiveRocks ObjectiveRocks is an Objective-C wrapper of Facebook's RocksDB - A Persistent Key-Value Store for Flash and RAM Storage. Current RocksD

A protocol-centric, type and queue safe key-value workflow.
A protocol-centric, type and queue safe key-value workflow.

Light-weight, strict protocol-first styled PropertyKit helps you to easily and safely handle guaranteed values, keys or types on various situations of

An efficient, small mobile key-value storage framework developed by WeChat. Works on Android, iOS, macOS, Windows, and POSIX.
An efficient, small mobile key-value storage framework developed by WeChat. Works on Android, iOS, macOS, Windows, and POSIX.

中文版本请参看这里 MMKV is an efficient, small, easy-to-use mobile key-value storage framework used in the WeChat application. It's currently available on Andr

A key value store for storing per-developer environment and application keys

A key value store for enviroment and application keys. Its good security practice to keep production keys out of developer hands. CocoaPods-keys makes

💾 Safe, statically-typed, store-agnostic key-value storage written in Swift!

Storez 💾 Safe, statically-typed, store-agnostic key-value storage Highlights Fully Customizable: Customize the persistence store, the KeyType class,

SwiftStore - Key/Value store for Swift backed by LevelDB.

SwiftStore Key/Value store for Swift backed by LevelDB. Usage Create instances of store import SwiftStore

YapDB is a collection/key/value store with a plugin architecture. It's built atop sqlite, for Swift & objective-c developers.
YapDB is a collection/key/value store with a plugin architecture. It's built atop sqlite, for Swift & objective-c developers.

YapDatabase is a collection/key/value store and so much more. It's built atop sqlite, for Swift & Objective-C developers, targeting macOS, iOS, tvOS &

💾 Safe, statically-typed, store-agnostic key-value storage written in Swift!

Storez 💾 Safe, statically-typed, store-agnostic key-value storage Highlights Fully Customizable: Customize the persistence store, the KeyType class,

Key-Value store for Swift backed by LevelDB

SwiftStore Key/Value store for Swift backed by LevelDB. Usage Create instances of store import SwiftStore // Create a store. let store = SwiftStore(s

A really simple key-value wrapper for keychain.

PlainKeychain A really simple key-value wrapper for keychain. Features ✅ Key-value pairs using kSecClassGenericPassword. ❌ Internet passwords (kSecCla

An elegant, fast, thread-safe, multipurpose key-value storage, compatible with all Apple platforms.
An elegant, fast, thread-safe, multipurpose key-value storage, compatible with all Apple platforms.

KeyValueStorage An elegant, fast, thread-safe, multipurpose key-value storage, compatible with all Apple platforms. Supported Platforms iOS macOS watc

Typed key-value storage solution to store Codable types in various persistence layers with few lines of code!
Typed key-value storage solution to store Codable types in various persistence layers with few lines of code!

🗂 Stores A typed key-value storage solution to store Codable types in various persistence layers like User Defaults, File System, Core Data, Keychain

Animate numeric value while setting new value to label
Animate numeric value while setting new value to label

NumericAnimatedLabel Example To run the example project, clone the repo, and run pod install from the Example directory first. Requirements Installati

C4 is an open-source creative coding framework that harnesses the power of native iOS programming with a simplified API that gets you working with media right away. Build artworks, design interfaces and explore new possibilities working with media and interaction. MusicScore - A music score library with MusicPart, Measure, Note, Pitch and Tempo representations in swift structs
MusicScore - A music score library with MusicPart, Measure, Note, Pitch and Tempo representations in swift structs

MusicScore A music score library with MusicPart, Measure, Note, Pitch and Tempo

Codable code is a Swift Package that allows you to convert JSON Strings into Swift structs

Codable code is a Swift Package that allows you to convert JSON Strings into Swift structs.

Delightful framework for iOS to easily persist structs, images, and data
Delightful framework for iOS to easily persist structs, images, and data

Installation • Usage • Debugging • A Word • Documentation • Apps Using Disk • License • Contribute • Questions? Disk is a powerful and simple file man

Easy BMI calculator to review optionals, segues and structs
Easy BMI calculator to review optionals, segues and structs

BMI Calculator Our Goal The goal of this tutorial is to learn more about Optionals, solidify your understanding of the MVC design pattern and to intro

Generate protobuf message definitions from Swift structs

SwiftProtobufGen Generates protobuf definitions from Swift structs Building For some reason Xcode builds don't work at all but swiftpm command line bu

Comments
Releases(4.0.0)
Owner
Brad Hilton
Brad Hilton
A Swift Runtime library for viewing type info, and the dynamic getting and setting of properties.

Runtime is a Swift library to give you more runtime abilities, including getting type metadata, setting properties via reflection, and type constructi

Wes Wickwire 970 Jan 3, 2023
Reflection based (Dictionary, CKRecord, NSManagedObject, Realm, JSON and XML) object mapping with extensions for Alamofire and Moya with RxSwift or ReactiveSwift

EVReflection General information At this moment the master branch is tested with Swift 4.2 and 5.0 beta If you want to continue using EVReflection in

Edwin Vermeer 964 Dec 14, 2022
Easy JSON to NSObject mapping using Cocoa's key value coding (KVC)

#Motis Object Mapping Easy JSON to NSObject mapping using Cocoa's key value coding (KVC) Motis is a user-friendly interface with Key Value Coding that

Mobile Jazz 249 Jun 29, 2022
RSA public/private key encryption, private key signing and public key verification in Swift using the Swift Package Manager. Works on iOS, macOS, and Linux (work in progress).

BlueRSA Swift cross-platform RSA wrapper library for RSA encryption and signing. Works on supported Apple platforms (using Security framework). Linux

Kitura 122 Dec 16, 2022
RSA public/private key encryption, private key signing and public key verification in Swift using the Swift Package Manager. Works on iOS, macOS, and Linux (work in progress).

BlueRSA Swift cross-platform RSA wrapper library for RSA encryption and signing. Works on supported Apple platforms (using Security framework). Linux

Kitura 122 Dec 16, 2022
A protocol to serialize Swift structs and classes for encoding and decoding.

Serpent (previously known as Serializable) is a framework made for creating model objects or structs that can be easily serialized and deserialized fr

Nodes - iOS 287 Nov 11, 2022
BFKit-Swift is a collection of useful classes, structs and extensions to develop Apps faster.

Features • Classes and Extensions Compatibility • Requirements • Communication • Contributing • Installing and Usage • Documentation • Changelog • Exa

Fabrizio Brancati 992 Dec 2, 2022
Core Data Generator (CDG for short) is a framework for generation (using Sourcery) of Core Data entities from plain structs/classes/enums.

Core Data Generator Introduction Features Supported platforms Installation CDG Setup RepositoryType ModelType DataStoreVersion MigrationPolicy Basic U

Lotusflare 18 Sep 19, 2022
Modern thread-safe and type-safe key-value observing for Swift and Objective-C

Now Archived and Forked PMKVObserver will not be maintained in this repository going forward. Please use, create issues on, and make PRs to the fork o

Postmates Inc. 708 Jun 29, 2022
Cachyr A typesafe key-value data cache for iOS, macOS, tvOS and watchOS written in Swift.

Cachyr A typesafe key-value data cache for iOS, macOS, tvOS and watchOS written in Swift. There already exists plenty of cache solutions, so why creat

Norsk rikskringkasting (NRK) 124 Nov 24, 2022