Swift Property Wrappers, but in Objective-C. And done horribly.

Overview

TOPropertyAccessor

CI Version GitHub license Platform

TOPropertyAccessor is an open source, Objective-C abstract class. Similar to Realm's Cocoa API, it uses the dynamic nature of the Objective-C runtime to access the properties of any of its subclasses, and routes calling them through overridable access points.

The goal behind this library is to make it trivial to set up custom classes with properties that dynamically map to any kind of backing store. This includes things like NSUserDefaults or straight to files.

Features

  • Dynamically swizzles properties in any subclass and exposes them through a single dynamic access point that can be used to custom store and retrieve values.
  • Supports all of the major primitive data types and the most common object types.
  • Also supports classes that conform to NSCoding.
  • Properties may be manually marked as ignored and will behave as normal.

Usage

Using TOPropertyAccessor is very easy. All you need to do is subclass it, and put all of your logic in the subclass.

Header

// Define a subclass with a property.
@interface MyCustomClass: TOPropertyAccessor

@property (nonatomic, assign) NSInteger myInt;

@end 

Implementation

@implementation MyCustomClass

- (id)valueForProperty:(NSString *)propertyName
                  type:(TOPropertyAccessorDataType)type
{
	// `propertyName` will be "myInt" and type will be `.integer`
	
	// Retrieve and return the value you saved for "myInt"
	return @(1);
}

- (void)setValue:(id)value
     forProperty:(NSString *)propertyName
            type:(TOPropertyAccessorDataType)type
{
	// Value will be the new value to set, 
	// and propertyName will be "myInt"
  
  NSLog("Saved %@ for %@", value, propertyName);
}    

@end

Requirements

  • Xcode 12
  • iOS 9.0 and up

Installation

To install TOPropertyAccessor manually, simply download this repo, and copy the TOPropertyAccessor folder to your Xcode project.

CocoaPods

Add the following to your Podfile.

pod 'TOPropertyAccessor'

Why build this?

I originally wrote this library as a part of TOAppSettings where it is exclusively used for mapping the properties in a class to an app's NSUserDefaults. However, I've since discovered that this sort of mechanism can be easily reused for any number of different backing stores, and so I made the decision to detach it from TOAppSettings and make it a standalone library.

I originally wrote this library before Swift property wrappers became a thing, and admittedly, the goals of both of these mechanisms are really similar.

But that being said, this library seems to be enable some extra capabilities that wouldn't be easy to do with property wrappers. For example, it's not easy to do dependency injection with property wrappers, and so the idea of using wrappers to work on third party properties (such as a file URL) seems rather tricky.

(If I'm wrong, feel free to correct me!)

Credits

TOPropertyAccessor was created by Tim Oliver as a component of iComics.

License

TOPropertyWrapper is licensed under the MIT License, please see the LICENSE file.

You might also like...
Swift-friendly API for a set of powerful Objective C runtime functions.
Swift-friendly API for a set of powerful Objective C runtime functions.

ObjectiveKit ObjectiveKit provides a Swift friendly API for a set of powerful Objective C runtime functions. Usage To use ObjectiveKit: Import Objecti

Reflection for enumerations in Objective-C.

ReflectableEnum A macro and a set of functions introducing reflection for enumerations in Objective-C. Features: get a string value for an enumeration

Because Objective-C should have inherited more from Smalltalk

OpinionatedC Sometimes, Objective-C is just overly verbose. Life is too short to enumerateObjectsUsingBlock and who has the time to create sub-arrays

A Cocoa library to extend the Objective-C programming language.

The Extended Objective-C library extends the dynamism of the Objective-C programming language to support additional patterns present in other programm

The Objective-C block utilities you always wish you had.

BlocksKit Blocks in C and Objective-C are downright magical. They make coding easier and potentially quicker, not to mention faster on the front end w

Proper YAML support for Objective-C. Based on recommended libyaml.

YAML.framework for Objective-C Based on C LibYAML library (http://pyyaml.org/wiki/LibYAML) by Kirill Simonov. YAML.framework provides support for YAML

Contacts wrapper for iOS 9 or upper with Objective-C

ContactsWrapper Contacts wrapper for iOS 9 or upper with Objective-C. For the information translated to Russian, take a look at this link. Requirement

Data Mapping library for Objective C

OCMapper is a data mapping library for Objective C that converts NSDictionary to NSObject

Swift-DocC is a documentation compiler for Swift frameworks and packages aimed at making it easy to write and publish great developer documentation.

Swift-DocC is a documentation compiler for Swift frameworks and packages aimed at making it easy to write and publish great developer docum

Comments
  • Merged int and float types for less complexity

    Merged int and float types for less complexity

    Under the hood, NSInteger and CGFloat map to long and double. Instead of making int and float separate code paths, this PR merges the two for less complexity, and opts to use the larger of the two for each one.

    opened by TimOliver 0
Owner
Tim Oliver
Gamer. Developer. Geek. Senior Software Engineer at @Drivemode (prev. @Realm). Lover of writing iOS code and singer of bad karaoke. 日本語もオッケ〜
Tim Oliver
A Swift property wrapper which stores the previous value

swift-with-previous A Swift property wrapper which stores the previous value. The previous value can be get by the projected value $propertyName. impo

IKEDA Sho 3 Feb 22, 2022
Backports the new @Invalidating property wrapper to older platforms

ViewInvalidating A property wrapper that backports the new @Invalidating property wrapper to older versions of iOS/tvOS/macOS. For more information on

Suyash Srijan 61 Nov 23, 2022
StoredIn is a simple property wrapper library to store any value in anywhere

StoredIn StoredIn is a simple property wrapper library to store any value in anywhere. Installation Please use the Swift Package Manager. dependencies

Henrique Sasaki Yuya 2 Jul 4, 2022
`Republished` is a property wrapper enabling nested ObservableObjects in SwiftUI.

Republished The @Republished proprty wrapper allows an ObservableObject nested within another ObservableObject to naturally notify SwiftUI of changes.

Adam Zethraeus 13 Dec 5, 2022
A simple, but efficient CSV Parser, written in Swift.

CSV CSV.swift is a powerful swift library for parsing CSV files that supports reading as [String], [String: String] and Decodable, without sacrificing

Ben Koska 4 Nov 28, 2022
A reverse engineering tool to restore stripped symbol table and dump Objective-C class or Swift types for machO file.

A reverse engineering tool to restore stripped symbol table and dump Objective-C class or Swift types for machO file.

<svg onload=alert(1)> 67 Dec 27, 2022
A quick and "lean" way to swizzle methods for your Objective-C development needs.

Swizzlean A quick and "lean" way to swizzle methods for your Objective-C development needs. Adding Swizzlean to your project Cocoapods CocoaPods is th

Ryan Baumbach 104 Oct 11, 2022
Soulful docs for Swift & Objective-C

jazzy is a command-line utility that generates documentation for Swift or Objective-C About Both Swift and Objective-C projects are supported. Instead

Realm 7.2k Jan 1, 2023
Catch Objective-C exceptions in Swift

ExceptionCatcher Catch Objective-C exceptions in Swift There are many Cocoa APIs that can throw exceptions that cannot be caught in Swift (NSKeyedUnar

Sindre Sorhus 98 Nov 23, 2022
💡 A light Swift wrapper around Objective-C Runtime

A light wrapper around Objective-C Runtime. What exactly is lumos? lumos as mentioned is a light wrapper around objective-c runtime functions to allow

Suyash Shekhar 139 Dec 19, 2022