Skip to content

Instantly share code, notes, and snippets.

@verma-manish58
Created July 27, 2016 06:04
Show Gist options
  • Save verma-manish58/6a2b955774e9412e227aa764a2e73733 to your computer and use it in GitHub Desktop.
Save verma-manish58/6a2b955774e9412e227aa764a2e73733 to your computer and use it in GitHub Desktop.
Then you can simply use the NSArray property defined on the UILabel just like any other pre-defined property as follows:
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 150, 150)];
[lbl setArrTitlesAssociativeObj:@[]];
lbl.arrTitlesAssociativeObj = @[];
//
// NSObject+AssociatedObject.h
// OCLearning
//
// Created by Manish Verma on 26/07/16.
// Copyright © 2016 Manish Verma. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface NSObject (AssociatedObject)
- (void)associateValue:(id)value forKey:(void*)key;
- (id)getAssociatedValueForKey:(void*)key;
@end
//
// NSObject+AssociatedObject.m
// OCLearning
//
// Created by Manish Verma on 26/07/16.
// Copyright © 2016 Manish Verma. All rights reserved.
//
#import "NSObject+AssociatedObject.h"
#import <objc/runtime.h>
@implementation NSObject (AssociatedObject)
- (void)associateValue:(id)value forKey:(void*)key {
objc_setAssociatedObject(self, &key, value, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (id)getAssociatedValueForKey:(void*)key {
return objc_getAssociatedObject(self, &key);
}
@end
//
// UILabel+CustomLabel.h
// OCLearning
//
// Created by Manish Verma on 26/07/16.
// Copyright © 2016 Manish Verma. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface UILabel (CustomLabel)
@property (nonatomic, retain) NSArray *arrTitlesAssociativeObj;
@end
//
// UILabel+CustomLabel.m
// OCLearning
//
// Created by Manish Verma on 26/07/16.
// Copyright © 2016 Manish Verma. All rights reserved.
//
#import "UILabel+CustomLabel.h"
#import "NSObject+AssociatedObject.h"
@implementation UILabel (CustomLabel)
- (void)setArrTitlesAssociativeObj:(NSArray *)arrTitlesAssociativeObj {
[self associateValue:arrTitlesAssociativeObj forKey:@selector(arrTitlesAssociativeObj)];
}
- (NSArray*)arrTitlesAssociativeObj {
return [self getAssociatedValueForKey:@selector(arrTitlesAssociativeObj)];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment