Last active
December 24, 2015 00:19
-
-
Save EliaCereda/982019c35f00b53dd4d5 to your computer and use it in GitHub Desktop.
Runtime instance variables for Objective-C classes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// CRZRuntimeIvars.h | |
// CRZRuntimeIvars | |
// | |
// Copyright (c) 2013-2015 Elia Cereda | |
// | |
// Permission is hereby granted, free of charge, to any person obtaining a copy | |
// of this software and associated documentation files (the "Software"), to deal | |
// in the Software without restriction, including without limitation the rights | |
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
// copies of the Software, and to permit persons to whom the Software is | |
// furnished to do so, subject to the following conditions: | |
// | |
// The above copyright notice and this permission notice shall be included in all | |
// copies or substantial portions of the Software. | |
// | |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
// SOFTWARE. | |
// | |
#import <Foundation/Foundation.h> | |
#import <objc/runtime.h> | |
// | |
// README | |
// | |
// This is the workflow to create an instance variable at runtime: | |
// 1. Import this file | |
// | |
// 2. Create the ivar at the beginning of your .m file | |
// CRZCreateIvar(class, name, associationPolicy) | |
// | |
// 3. Enjoy! | |
// CRZIvarGetValue(class, object, name) | |
// CRZIvarSetValue(class, object, name, value) | |
// | |
// If you need the same ivar in more than one file, create it in one .m | |
// file and load it in the others with: | |
// CRZExternIvar(class, name) | |
// | |
// The values in this enum have the same meaning as the attributes of a @property. | |
typedef NS_ENUM(objc_AssociationPolicy, CRZAssociationPolicy) { | |
CRZAssociationAssign = OBJC_ASSOCIATION_ASSIGN, | |
CRZAssociationRetain = OBJC_ASSOCIATION_RETAIN, | |
CRZAssociationRetainNonAtomic = OBJC_ASSOCIATION_RETAIN_NONATOMIC, | |
CRZAssociationCopy = OBJC_ASSOCIATION_COPY, | |
CRZAssociationCopyNonAtomic = OBJC_ASSOCIATION_COPY_NONATOMIC, | |
}; | |
typedef struct { | |
// No value is actually stored in this variable, the API just uses its memory location. | |
char identifier; | |
CRZAssociationPolicy associationPolicy; | |
} _CRZIvar; | |
#define CRZIvar(class, name) \ | |
(CRZIvar_ ## class ## _ ## name) | |
#define CRZCreateIvar(class, name, _associationPolicy) \ | |
_CRZIvar CRZIvar(class, name) = { \ | |
.associationPolicy = _associationPolicy \ | |
}; | |
// Use this if you want to use an ivar created in another file | |
#define CRZExternIvar(class, name) \ | |
extern _CRZIvar CRZIvar(class, name); | |
#define CRZIvarGetValue(class, object, name) \ | |
(objc_getAssociatedObject( \ | |
object, &CRZIvar(class, name).identifier \ | |
)) | |
#define CRZIvarSetValue(class, object, name, value) \ | |
(objc_setAssociatedObject( \ | |
object, &CRZIvar(class, name).identifier, \ | |
value, CRZIvar(class, name).associationPolicy \ | |
)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment