Syntax Help Required (use of a string variable)
-
25-07-2021 - |
Question
I'm doing something slightly or monumentally wrong.
I want to swap a UIColour for a programmable variable, for example:
NSString *DataColourTitle = @"greenColor";
[[UINavigationBar appearance] setTintColor: [UIColor [NSString stringWithFormat: @"%@", DataColourTitle]]];
I'm getting an Expected Identifier error at the NSString part and haven't worked out the formatting required to accomplish this.
Solution
The syntax for calling a class method of UIColor would be [UIColor <#some selector#>]
where <#some selector#> is an identifier such as greenColor
, not a string. The selector that it expects is a reference to code that can return a UIColor object, whereas @"greenColor"
is just a static, literal string of bytes wrapped up as a NSString object.
Try something like:
SEL green = NSSelectorFromString(@"greenColor");
UIColor *color = [UIColor performSelector:green];
[[UINavigationBar appearance] setTintColor: color];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow