删除@“"从NSString或将NSString类型转换为变量名 [英] Remove @"" from NSString or typecast NSString into variable name
问题描述
我从文件中读取了NSString,然后我想将其定义为UIColor,以便快速更改颜色.
I read a NSString from file and then I want to #define this as a UIColor so I can quickly change color's.
我希望这样工作:
#define GRAY [UIColor darkGrayColor]
然后从文件中读取字符串:@"GRAY"并将其放入名为kColor的NSString变量中
Then read from file the string : @"GRAY" and put it in a NSString variable called kColor
然后将其粘贴到backgroundcolor属性中,如下所示:
And then paste this onto a backgroundcolor property like so:
myController.view.backgroundColor = kColor;
现在这意味着我得到了:
now this means I get :
myController.view.backgroundColor = @"GRAY";
我需要:
myController.view.backgroundColor = GRAY;
,以使#define起作用.那么,如何从字符串中删除@"或将字符串类型转换为变量名呢?我不拘泥于#define,因此,如果还有其他方法可以使它起作用,那么我会持开放态度.
for the #define to work. So how do i remove @"" from the string or typecast the string into a variable name? I'm not stuck on #define so if there is another way to get this to work, I'm open to that.
非常感谢您的帮助.
推荐答案
您可以将颜色加载到NSDictionary
中.
You could load the colors into a NSDictionary
.
// Put this in some global scope
NSDictionary *colors = [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor darkGrayColor], @"GRAY", nil];
// using the dictionary
myController.view.backgroundColor = [colors objectForKey:kColor];
这篇关于删除@“"从NSString或将NSString类型转换为变量名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!