使用 ivar 名称重构 iPhone
谁能告诉我如何在我的方法中动态指定 ivar 名称? l2 是我想要瞄准的 ivar。
//this works
if (maxunlocked > 1) {
filename = [NSString stringWithFormat:@"level%d.png", [[fliesArray objectAtIndex:2] intValue]];
filenameHi = [NSString stringWithFormat:@"level%dHi.png", [[fliesArray objectAtIndex:2] intValue]];
l2 = [SoundMenuItem itemFromNormalSpriteFrameName:filename selectedSpriteFrameName:filenameHi target:self selector:@selector(level:)];
}
//这不是
for (int i = 0; i<11; i++) {
if (maxunlocked > i) {
filename = [NSString stringWithFormat:@"level%d.png", [[fliesArray objectAtIndex:i] intValue]];
filenameHi = [NSString stringWithFormat:@"level%dHi.png", [[fliesArray objectAtIndex:i] intValue]];
//this is where I'm attempting to dynamically specify the SoundMenuItem instance name.
sndMenuItem = [NSString stringWithFormat:@"l%d", i];
sndMenuItem = [SoundMenuItem itemFromNormalSpriteFrameName:filename selectedSpriteFrameName:filenameHi target:self selector:@selector(level:)];
sndMenuItem.userData = (id)i;
}
}
谢谢,
马克
Could anyone tell me how I can dynamically specify an ivar name within my method?
l2 is the ivar I'm trying to target.
//this works
if (maxunlocked > 1) {
filename = [NSString stringWithFormat:@"level%d.png", [[fliesArray objectAtIndex:2] intValue]];
filenameHi = [NSString stringWithFormat:@"level%dHi.png", [[fliesArray objectAtIndex:2] intValue]];
l2 = [SoundMenuItem itemFromNormalSpriteFrameName:filename selectedSpriteFrameName:filenameHi target:self selector:@selector(level:)];
}
//this doesn't
for (int i = 0; i<11; i++) {
if (maxunlocked > i) {
filename = [NSString stringWithFormat:@"level%d.png", [[fliesArray objectAtIndex:i] intValue]];
filenameHi = [NSString stringWithFormat:@"level%dHi.png", [[fliesArray objectAtIndex:i] intValue]];
//this is where I'm attempting to dynamically specify the SoundMenuItem instance name.
sndMenuItem = [NSString stringWithFormat:@"l%d", i];
sndMenuItem = [SoundMenuItem itemFromNormalSpriteFrameName:filename selectedSpriteFrameName:filenameHi target:self selector:@selector(level:)];
sndMenuItem.userData = (id)i;
}
}
Thanks,
Mark
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您将其声明为属性,则可以使用 KVC 来获取它。
[编辑]
我没听懂你在说什么。答案是否定的。您无法动态指定变量名称。你可以做的是:
[编辑]
你可能应该重新构建你的整个班级。
If you have it declared as a property, you may be able to use KVC to get it.
[EDIT]
I didn't understand what you're saying. The answer is no. You can't specify a variable name dynamically. What you can do is this:
[EDIT]
You should probably re-structure your entire class.
抱歉缺少信息,感谢您的耐心等待。由于字符限制,我很难将其全部添加到评论区。
这就是我正在尝试的...
在我的头文件中:
//SoundMenuItem 是一个类
声音菜单项 *l1;
声音菜单项 *l2; (我实际上有 20 个按钮,每个游戏级别一个)
在我的 .m 文件中
//现在我检查 level2 是否已解锁(maxunlocked > 1),如果是,我重置 l2 实例以使用不同的图像。
if (maxunlocked > 1) {
filename = [NSString stringWithFormat:@"level%d.png", [[fliesArray objectAtIndex:2] intValue]];
filenameHi = [NSString stringWithFormat:@"level%dHi.png", [[fliesArray objectAtIndex:2] intValue]];
l2 = [SoundMenuItem itemFromNormalSpriteFrameName:文件名 selectedSpriteFrameName:文件名Hi 目标:self 选择器:@selector(level:)];
因此,
我不想对上述 if 语句进行 20 次迭代,而是每个按钮迭代一次,我想将其重构为一次。
我希望我已经说得更清楚了。
Sorry about the lack of info and thanks for your patience. I was struggling to add it all into the comments area due to the character limit.
This is what I'm attempting...
In my header file:
//SoundMenuItem is a class
SoundMenuItem *l1;
SoundMenuItem *l2; (I actually have 20 buttons, one for each game level)
In my .m file
//now I check to see if level2 has been unlocked (maxunlocked > 1) and if so I reset the l2 instance to use different images.
if (maxunlocked > 1) {
filename = [NSString stringWithFormat:@"level%d.png", [[fliesArray objectAtIndex:2] intValue]];
filenameHi = [NSString stringWithFormat:@"level%dHi.png", [[fliesArray objectAtIndex:2] intValue]];
l2 = [SoundMenuItem itemFromNormalSpriteFrameName:filename selectedSpriteFrameName:filenameHi target:self selector:@selector(level:)];
}
So rather than having 20 iterations of the above if statement, one for each button I'm wanting to refactor it into one.
I hope I've made myself clearer.
问题澄清后,我的第一个答案无效。 @stephen 的答案以及使用字典来保留所有 SoundMenuItem 的建议也是我所提议的。
After the question was clarified my first answer was not valid. The answer by @stephen with the suggestion for using a dictionary to keeping all the SoundMenuItem is what I would have proposed as well.