[英]How to store not id type variable use a objc_getAssociatedObject/objc_setAssociatedObject?
[英]How do I use objc_setAssociatedObject/objc_getAssociatedObject inside an object?
如果我在类别实现中使用objc_setAssociatedObject / objc_getAssociatedObject将模拟实例变量存储在setter方法中,那么我将如何访问getter方法中的键,因为setter方法中声明的任何变量都不在getter方法的范围内?
编辑:为了澄清,如果我使用以下模式,我应该在哪里声明STRING_KEY,以便我可以在setter和getter方法中使用它。
@interface NSView (simulateVar)
-(void)setSimualtedString:(NSString *)myString;
-(NSString *)simulatedString;
@end
@implementation NSView (simulateVar)
-(void)setSimualtedString: (NSString *)myString
{
objc_setAssociatedObject(self, &STRING_KEY, myString, OBJC_ASSOCIATION_RETAIN);
}
-(NSString *)simulatedString
{
return (NSString *)objc_getAssociatedObject(self, &STRING_KEY);
}
@end
声明一个静态变量,以便您可以使用其地址作为键。 对objc_setAssociatedObject的调用采用void *,实际只使用静态变量的地址,而不是NSString的内容......只是浪费内存。
你只需要添加:
static char STRING_KEY; // global 0 initialization is fine here, no
// need to change it since the value of the
// variable is not used, just the address
我知道这个问题已经过时了,但我认为完整性还有另一种方法可以使用值得一提的相关对象。 该解决方案使用@selector
,因此不需要任何额外的变量或常量。
@interface NSObject (CategoryWithProperty)
@property (nonatomic, strong) NSObject *property;
@end
@implementation NSObject (CategoryWithProperty)
- (NSObject *)property {
return objc_getAssociatedObject(self, @selector(property));
}
- (void)setProperty:(NSObject *)value {
objc_setAssociatedObject(self, @selector(property), value, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
@end
(受http://www.tuaw.com/2013/04/10/devjuice-better-objective-c-associated-objects/启发)
对于关联存储void *
键,我喜欢这样做:
static void * const kMyAssociatedStorageKey = (void*)&kMyAssociatedStorageKey;
这避免了在可执行文件中有另一个常量字符串,并通过将其值设置为自身的地址,您可以获得良好的单一性和const
行为(因此,如果您执行某些会改变其值的事情,您将在名义上获得编译器投诉键),没有任何额外的不必要的导出符号。
在源文件的顶级声明一个静态(编译单元范围)变量。 它可能有助于使其有意义,如下所示:
static NSString *MYSimulatedString = @"MYSimulatedString";
相当接近。 这是一个完整的例子。
.H文件
@interface NSObject (ExampleCategoryWithProperty)
@property (nonatomic, retain) NSArray *laserUnicorns;
@end
.M文件
#import <objc/runtime.h>
static void * LaserUnicornsPropertyKey = &LaserUnicornsPropertyKey;
@implementation NSObject (ExampleCategoryWithProperty)
- (NSArray *)laserUnicorns {
return objc_getAssociatedObject(self, LaserUnicornsPropertyKey);
}
- (void)setLaserUnicorns:(NSArray *)unicorns {
objc_setAssociatedObject(self, LaserUnicornsPropertyKey, unicorns, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
@end
就像一个普通的财产 - 可用点符号访问
NSObject *myObject = [NSObject new];
myObject.laserUnicorns = @[@"dipwit", @"dipshit"];
NSLog(@"Laser unicorns: %@", myObject.laserUnicorns);
语法更简单
或者,您可以使用@selector(nameOfGetter)
而不是创建静态指针。 为什么? 请参阅https://stackoverflow.com/a/16020927/202451 。 例:
- (NSArray *)laserUnicorns {
return objc_getAssociatedObject(self, @selector(laserUnicorns));
}
- (void)setLaserUnicorns:(NSArray *)unicorns {
objc_setAssociatedObject(self, @selector(laserUnicorns), unicorns, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
已接受的答案已经正确,但我想补充说明:“关键”的要点是获得一个不会发生意外碰撞的唯一(每个进程/程序)标识符。
试想一下,关键被宣布为NSUInteger
:很多人会使用标准值一样0
, 1
或42
。 特别是如果您使用某些库或框架,可能会发生冲突。
因此,一些聪明的Apple工程师有想法将密钥声明为指针,其意图是声明变量并将指针作为键传递给该变量 。 链接器将为该变量分配一个在您的应用程序中唯一的地址,因此您可以保证获得唯一的无冲突值。
实际上,你可以传递你喜欢的任何值,指针不会被解引用(我已经测试过了)。 所以传递(void *)0
或(void *)42
作为键确实有用,虽然这不是一个好主意(所以请不要这样做)。 对于objc_set/getAssociatedObject
,重要的是作为键传递的值在您的进程/程序中是唯一的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.