Permalink
Browse files

Wrap associated objects in a nice API

So we don't have to import the entire Objective-C runtime to do this stuff.
  • Loading branch information...
1 parent ac67318 commit c5a8c8142f66e583873f37ff6ec63f30bd6b6c49 Brent Royal-Gordon committed Nov 12, 2012
Showing with 60 additions and 14 deletions.
  1. +4 −3 ANEntity.m
  2. +4 −3 ANPost.m
  3. +7 −6 ANResource+Magic.m
  4. +2 −2 ANResource.m
  5. +17 −0 NSObject+AssociatedObject.h
  6. +26 −0 NSObject+AssociatedObject.m
View
@@ -8,7 +8,7 @@
#import "ANEntity.h"
-#import <objc/runtime.h>
+#import "NSObject+AssociatedObject.h"
const NSRange ANEntityNoRange = (NSRange){ NSNotFound, 0 };
@@ -43,7 +43,8 @@ - (NSArray *)links {
}
- (NSArray *)all {
- NSArray * ret = objc_getAssociatedObject(self, _cmd);
+ // We use associated objects here because associated objects are cleared when the representation is set.
+ NSArray * ret = [self associatedObjectForKey:_cmd];
if(!ret) {
NSMutableArray * allEntities = [NSMutableArray new];
@@ -84,7 +85,7 @@ - (NSArray *)all {
ret = allEntities.copy;
- objc_setAssociatedObject(self, _cmd, ret, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
+ [self setAssociatedObject:ret forKey:_cmd];
}
return ret;
View
@@ -7,7 +7,7 @@
//
#import "ANPost.h"
-#import <objc/runtime.h>
+#import "NSObject+AssociatedObject.h"
@implementation ANPost
@@ -38,10 +38,11 @@ @implementation ANPost
@dynamic repostOfRepresentation;
- (ANAnnotationSet *)annotations {
- ANAnnotationSet * annotations = objc_getAssociatedObject(self, _cmd);
+ // We use an associated object here so that setRepresentation: will clear it.
+ ANAnnotationSet * annotations = [self associatedObjectForKey:_cmd];
if(!annotations) {
annotations = [[ANAnnotationSet alloc] initWithRepresentation:self.annotationRepresentations session:self.session];
- objc_setAssociatedObject(self, _cmd, annotations, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
+ [self setAssociatedObject:annotations forKey:_cmd];
}
return annotations;
}
View
@@ -8,6 +8,7 @@
#import "ANResource+Magic.h"
#import <objc/runtime.h>
+#import "NSObject+AssociatedObject.h"
// This category is responsible for ANResource's magically appearing accessors.
//
@@ -128,13 +129,13 @@ static id ANBlockForGetterReturningType(NSString * name, NSString * propType) {
SEL _cmd = NSSelectorFromString(name);
return ^NSDate*(ANResource * self) {
- id object = objc_getAssociatedObject(self, _cmd);
+ id object = [self associatedObjectForKey:_cmd];
if(!object) {
object = [self.representation objectForKey:key];
object = [ANResource.dateFormatter dateFromString:object];
- objc_setAssociatedObject(self, _cmd, object, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
+ [self setAssociatedObject:object forKey:_cmd];
}
return object;
@@ -162,13 +163,13 @@ static id ANBlockForGetterReturningType(NSString * name, NSString * propType) {
SEL _cmd = NSSelectorFromString(name);
return ^ANResource*(ANResource * self) {
- id object = objc_getAssociatedObject(self, _cmd);
+ id object = [self associatedObjectForKey:_cmd];
if(!object) {
object = [self.representation objectForKey:key];
object = [[class alloc] initWithRepresentation:object session:self.session];
- objc_setAssociatedObject(self, _cmd, object, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
+ [self setAssociatedObject:object forKey:_cmd];
}
return object;
@@ -190,7 +191,7 @@ static id ANBlockForGetterReturningType(NSString * name, NSString * propType) {
SEL _cmd = NSSelectorFromString(name);
return ^unsigned long long(ANResource * self) {
- id object = objc_getAssociatedObject(self, _cmd);
+ id object = [self associatedObjectForKey:_cmd];
if(!object) {
object = [self.representation objectForKey:key];
@@ -199,7 +200,7 @@ static id ANBlockForGetterReturningType(NSString * name, NSString * propType) {
object = [ANResource.IDFormatter numberFromString:object];
}
- objc_setAssociatedObject(self, _cmd, object, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
+ [self setAssociatedObject:object forKey:_cmd];
}
return [object unsignedLongLongValue];
View
@@ -11,7 +11,7 @@
#import "ANSession.h"
#import "ANSession+ANResource_Private.h"
-#import <objc/runtime.h>
+#import "NSObject+AssociatedObject.h"
NSString * const ANResourceDidUpdateNotification = @"ANResourceDidUpdate";
@@ -61,7 +61,7 @@ - (void)setRepresentation:(NSDictionary *)representation {
[self willChangeValueForKey:@"representation"];
_representation = representation;
- objc_removeAssociatedObjects(self);
+ [self removeAllAssociatedObjects];
[self didChangeValueForKey:@"representation"];
@@ -0,0 +1,17 @@
+//
+// NSObject+AssociatedObject.h
+// Alef
+//
+// Created by Brent Royal-Gordon on 11/11/12.
+// Copyright (c) 2012 Architechies. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+@interface NSObject (AssociatedObject)
+
+- (id)associatedObjectForKey:(void*)key;
+- (void)setAssociatedObject:(id)object forKey:(void*)key;
+- (void)removeAllAssociatedObjects;
+
+@end
@@ -0,0 +1,26 @@
+//
+// NSObject+AssociatedObject.m
+// Alef
+//
+// Created by Brent Royal-Gordon on 11/11/12.
+// Copyright (c) 2012 Architechies. All rights reserved.
+//
+
+#import "NSObject+AssociatedObject.h"
+#import <objc/runtime.h>
+
+@implementation NSObject (AssociatedObject)
+
+- (id)associatedObjectForKey:(void *)key {
+ return objc_getAssociatedObject(self, key);
+}
+
+- (void)setAssociatedObject:(id)object forKey:(void *)key {
+ objc_setAssociatedObject(self, key, object, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
+}
+
+- (void)removeAllAssociatedObjects {
+ objc_removeAssociatedObjects(self);
+}
+
+@end

0 comments on commit c5a8c81

Please sign in to comment.