I use a category, and some variable stored in a UIView.
but only stored id type, so I really want none id type (int, float, double, char… and so on)
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: