PageRenderTime 12ms CodeModel.GetById 8ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/BlocksKit/NSObject+AssociatedObjects.h

http://github.com/zwaldowski/BlocksKit
C++ Header | 114 lines | 13 code | 13 blank | 88 comment | 0 complexity | 1bf4d9a6df1564a11ab84ccbffeca9e1 MD5 | raw file
  1//
  2//  NSObject+AssociatedObjects.h
  3//  %PROJECT
  4//
  5
  6#import "BKGlobals.h"
  7
  8/** Objective-C wrapper for 10.6 associated object API.
  9
 10 In Mac OS X Snow Leopard and iOS 3.0, Apple introduced an
 11 addition to the Objective-C Runtime called associated objects.
 12 Associated objects allow for the pairing of a random key and
 13 object pair to be saved on an instance.
 14
 15 In BlocksKit, associated objects allow us to emulate instance
 16 variables in the categories we use.
 17 
 18 Class methods also exist for each association. These associations
 19 are unique to each class, and exist for the lifetime of the
 20 application unless set to `nil`. Each class is a unique meta-object;
 21 the ultimate singleton.
 22
 23 Created by Andy Matuschak as [AMAssociatedObjects](https://github.com/andymatuschak/NSObject-AssociatedObjects).
 24 Licensed in the public domain.
 25 */
 26@interface NSObject (AssociatedObjects)
 27
 28/** Strongly associates an object with the reciever.
 29
 30 The associated value is retained as if it were a property
 31 synthesized with `nonatomic` and `retain`.
 32
 33 Using retained association is strongly recommended for most
 34 Objective-C object derivative of NSObject, particularly
 35 when it is subject to being externally released or is in an
 36 `NSAutoreleasePool`.
 37
 38 @param value Any object.
 39 @param key A unique key pointer.
 40 */
 41- (void)associateValue:(id)value withKey:(const char *)key;
 42
 43/** Strongly associates an object with the receiving class.
 44 
 45 @see associateValue:withKey:
 46 @param value Any object.
 47 @param key A unique key pointer.
 48 */
 49+ (void)associateValue:(id)value withKey:(const char *)key;
 50
 51/** Associates a copy of an object with the reciever.
 52
 53 The associated value is copied as if it were a property
 54 synthesized with `nonatomic` and `copy`.
 55
 56 Using copied association is recommended for a block or
 57 otherwise `NSCopying`-compliant instances like NSString.
 58
 59 @param value Any object, pointer, or value.
 60 @param key A unique key pointer.
 61 */
 62- (void)associateCopyOfValue:(id)value withKey:(const char *)key;
 63
 64/** Associates a copy of an object with the receiving class.
 65 
 66 @see associateCopyOfValue:withKey:
 67 @param value Any object, pointer, or value.
 68 @param key A unique key pointer.
 69 */
 70+ (void)associateCopyOfValue:(id)value withKey:(const char *)key;
 71
 72/** Weakly associates an object with the reciever.
 73
 74 A weak association will cause the pointer to be set to zero
 75 or nil upon the disappearance of what it references;
 76 in other words, the associated object is not kept alive.
 77
 78 @param value Any object.
 79 @param key A unique key pointer.
 80 */
 81- (void)weaklyAssociateValue:(id)value withKey:(const char *)key;
 82
 83/** Weakly associates an object with the receiving class.
 84 
 85 @see weaklyAssociateValue:withKey:
 86 @param value Any object.
 87 @param key A unique key pointer.
 88 */
 89+ (void)weaklyAssociateValue:(id)value withKey:(const char *)key;
 90
 91/** Returns the associated value for a key on the reciever.
 92
 93 @param key A unique key pointer.
 94 @return The object associated with the key, or `nil` if not found.
 95 */
 96- (id)associatedValueForKey:(const char *)key;
 97
 98/** Returns the associated value for a key on the receiving class.
 99 
100 @see associatedValueForKey:
101 @param key A unique key pointer.
102 @return The object associated with the key, or `nil` if not found.
103 */
104+ (id)associatedValueForKey:(const char *)key;
105
106/** Returns the reciever to a clean state by removing all
107 associated objects, releasing them if necessary. */
108- (void)removeAllAssociatedObjects;
109
110/** Returns the recieving class to a clean state by removing
111 all associated objects, releasing them if necessary. */
112+ (void)removeAllAssociatedObjects;
113
114@end