1

Can I use somehow parameter of type SEL with objc_setAssociatedObject without converting it to NSString type?

Currently I'm using this code: objc_setAssociatedObject(thirdPartyObject, &kSelectorKey, NSStringFromSelector(selector), OBJC_ASSOCIATION_RETAIN);

Conversion to NSString works, but in feels wrong. There must be more appropriate solution.

Krypt
  • 434
  • 4
  • 12

2 Answers2

0

OBJC_EXPORT void objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy)

The 3rd parameter needs to be an object type. So, yes, you need that conversion.

bbum
  • 162,346
  • 23
  • 271
  • 359
  • underlaying type of SEL is const char*. Technically, it is const void*. But it cannot be retained, because it don't have an ISA. in iOS 9.2 id defined like `OBJC_EXPORT void objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy) __OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_1);`, by the way – Krypt Apr 20 '16 at 19:56
  • 1
    Sorry, stackoverflow reacts on some strange way on "Enter" key press. So, seems like you are right. – Krypt Apr 20 '16 at 20:22
0

Try This.
In my example i have passed indexpath you have to replace with your para meter

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    yourCustomeCell *aCell;
    NSString *aStrIdentifier = @"yourIdentiFier";
    aCell = (yourCustomeCell *)[tableView dequeueReusableCellWithIdentifier:aStrIdentifier];

    //you have to set your indexpath
    objc_setAssociatedObject(aCell.btnUpload_or_Add, @"objBtn", indexPath, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [aCell.YourButton addTarget:self action:@selector(yourButtonActiontapped:) forControlEvents:UIControlEventTouchUpInside];

    return aCell;
}

-(IBAction)yourButtonActiontapped:(UIButton *)sender{

    NSIndexPath *aIndPath =  objc_getAssociatedObject(sender, @"objBtn");
    NSLog(@"row:%@",aIndPath.row);
}

also you have to import #import <objc/runtime.h>

Vvk
  • 4,031
  • 29
  • 51