简体   繁体   English

如何存储非id类型变量使用objc_getAssociatedObject / objc_setAssociatedObject?

[英]How to store not id type variable use a objc_getAssociatedObject/objc_setAssociatedObject?

I use a category, and some variable stored in a UIView. 我使用了一个类别,并将一些变量存储在UIView中。

but only stored id type, so I really want none id type (int, float, double, char... and so on) 但只存储了id类型,所以我真的不想要id类型(int,float,double,char ......等等)

How to write a code? 如何编写代码?

#import <UIKit/UIKit.h>
#import <objc/runtime.h>

@interface UIView (CountClip)
@property (nonatomic, copy) NSString *stringTag;
@property (nonatomic) float offsetY;
@end

#import "UIView+CountClip.h"

@implementation UIView (CountClip)

static NSString *kStringTagKey = @"StringTagKey";

- (NSString *)stringTag
{
    return (NSString *)objc_getAssociatedObject(self, kStringTagKey);
}
- (void)setStringTag:(NSString *)stringTag
{
    objc_setAssociatedObject(self, kStringTagKey, stringTag, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (float)offsetY
{
    // how to write a correct code?
    return objc_getAssociatedObject(self, <#what is a code?#>);
}

- (void)setOffsetY:(float)offsetY
{
    // how to write a correct code?
    objc_setAssociatedObject(self, <#what is a code?#>,...);
}

@end

You need to convert floats to NSNumber instances and vice versa: 您需要将浮点数转换为NSNumber实例,反之亦然:

static NSString *kOffsetYTagKey = @"OffsetYTagKey";

- (float)offsetY
{
    // Retrieve NSNumber object associated with self and convert to float value
    return [objc_getAssociatedObject(self, kOffsetYTagKey) floatValue];
}

- (void)setOffsetY:(float)offsetY
{
    // Convert float value to NSNumber object and associate with self
    objc_setAssociatedObject(self, kOffsetYTagKey, [NSNumber numberWithFloat:offsetY],  OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

To store variables such as CGPoint , CGRect or CGSize , have a look at NSValue class and use appropriate method: 要存储CGPointCGRectCGSize等变量, CGRect查看NSValue类并使用适当的方法:

NSValue *value;
value = [NSValue valueWithCGPoint:point];
value = [NSValue valueWithCGRect:rect];
value = [NSValue valueWithCGSize:size];

Then convert it back via: 然后将其转换回来:

NSValue *value = ...;
CGPoint point = value.CGPointValue;
CGRect rect = value.CGRectValue;
CGSize size = value.CGSizeValue;

FYI: 供参考:

A much faster way of setting and getting associated objects could be this: 设置和获取关联对象的更快的方法可能是:

objc_setAssociatedObject(self, @selector(stringTag), stringTag, OBJC_ASSOCIATION_COPY_NONATOMIC);
objc_getAssociatedObject(self, @selector(stringTag));

This way you don't have to create the kStringTagKey variable. 这样您就不必创建kStringTagKey变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 没有匹配的函数在iOS中调用objc_getassociatedobject - No matching function to call objc_getassociatedobject in ios objc_getAssociatedObject返回错误的值 - objc_getAssociatedObject returns the wrong value objc_setAssociatedObject在iPhone模拟器中不可用 - objc_setAssociatedObject unavailable in iPhone simulator 使用objc_setAssociatedObject为UITableViewCell传递UIButton @selector参数 - using objc_setAssociatedObject to pass UIButton @selector argument for UITableViewCell 使用objc_setAssociatedObject时,体系结构armv7的未定义符号 - Undefined symbols for architecture armv7 when working with objc_setAssociatedObject 什么是objc_setAssociatedObject()以及在什么情况下应该使用它? - What is objc_setAssociatedObject() and in what cases should it be used? 声明两个属性,并使用相同的键调用objc_setAssociatedObject - declare two property and call objc_setAssociatedObject with same key objc_setAssociatedObject上的EXC_BAD_ACCESS与-weak_library /usr/lib/libSystem.B.dylib链接器标志 - EXC_BAD_ACCESS on objc_setAssociatedObject with -weak_library /usr/lib/libSystem.B.dylib linker flags 我如何获得objc_property_t类型 - how can i get objc_property_t type 在 objc 中需要 24 位类型 - need a 24 bits type in objc
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM