Question

Given a class with a property such as:

@property(nonatomic,retain) NSString* un_conventional;

I tried forming a selector like so:

SEL setterMethod =
  NSSelectorFromString(
    [NSString stringWithFormat:@"set%@", [key capitalizedString]]
  );
if ([customer respondsToSelector:setterMethod]) {
    //do something
}

PS: Just take it for granted that key is a variable with the value un_conventional

The problem seems to be that this doesn't match the correct setter method because capitalizedString causes setterMethod to be named setUn_Conventional instead of setUn_conventional ... the letter "c" is also capitalized!

So does anyone have any suggestions on how to dynamically prepare a selector for such a scenario such that it results in a selector named setUn_conventional and can be used to match via respondsToSelector?

Was it helpful?

Solution

SEL setterMethod = NSSelectorFromString(
    [NSString stringWithFormat:
          @"set%@%@",
          [[key substringToIndex:1]uppercaseString],
          [key substringFromIndex:1]
    ]
);

would do the trick if you can guarantee that key is at least 1 character long.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top