【发布时间】:2014-02-09 11:42:10
【问题描述】:
我有几个键定义为静态变量:
static NSString icon_0 = @"image_0.png";
static NSString icon_1 = @"some_image_with_a_different_name.png";
static NSString icon_3 = @"picure_of_a_bear.png";
现在在我获取索引路径的数据源方法中,我想从字符串创建变量名:
-(UICollectionviewCell*)cellForIndexPath:(NSIndexPath *)path
{
NSString *varName = [NSString stringWithFormat:@"icon_%d",path.row];
// here I need the static NSString which corresponds to the var name created
// i.e
NSString imageName;
if (indexPath.row == 0)
{
imageName = @"image_0.png";
}
// would be much nicer to do something like
NSString *imageName = [varName evaluate]; // get the content out of it...
}
How can I do this on static variable?
我试过了
NSString *iconName = [self valueForKey:str];
但它不是 iVar,所以无法正常工作...
【问题讨论】:
标签: ios objective-c runtime objective-c-runtime