1. <small id='Sbf0V'></small><noframes id='Sbf0V'>

      <legend id='Sbf0V'><style id='Sbf0V'><dir id='Sbf0V'><q id='Sbf0V'></q></dir></style></legend>

        <bdo id='Sbf0V'></bdo><ul id='Sbf0V'></ul>

      <i id='Sbf0V'><tr id='Sbf0V'><dt id='Sbf0V'><q id='Sbf0V'><span id='Sbf0V'><b id='Sbf0V'><form id='Sbf0V'><ins id='Sbf0V'></ins><ul id='Sbf0V'></ul><sub id='Sbf0V'></sub></form><legend id='Sbf0V'></legend><bdo id='Sbf0V'><pre id='Sbf0V'><center id='Sbf0V'></center></pre></bdo></b><th id='Sbf0V'></th></span></q></dt></tr></i><div id='Sbf0V'><tfoot id='Sbf0V'></tfoot><dl id='Sbf0V'><fieldset id='Sbf0V'></fieldset></dl></div>
      <tfoot id='Sbf0V'></tfoot>

      使用 NSString 制作变量名

      Make variable name out of NSString(使用 NSString 制作变量名)

      <small id='e50px'></small><noframes id='e50px'>

      <tfoot id='e50px'></tfoot>
          <tbody id='e50px'></tbody>

                <bdo id='e50px'></bdo><ul id='e50px'></ul>
                <legend id='e50px'><style id='e50px'><dir id='e50px'><q id='e50px'></q></dir></style></legend>
                <i id='e50px'><tr id='e50px'><dt id='e50px'><q id='e50px'><span id='e50px'><b id='e50px'><form id='e50px'><ins id='e50px'></ins><ul id='e50px'></ul><sub id='e50px'></sub></form><legend id='e50px'></legend><bdo id='e50px'><pre id='e50px'><center id='e50px'></center></pre></bdo></b><th id='e50px'></th></span></q></dt></tr></i><div id='e50px'><tfoot id='e50px'></tfoot><dl id='e50px'><fieldset id='e50px'></fieldset></dl></div>

                本文介绍了使用 NSString 制作变量名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一个 for 循环,其中必须创建 9 个六边形(hexagon1 到 hexagon9)...但是我不能使用 hexString 作为 Sprite 的名称,因为它是一个 NSString,对吗?那么我该怎么做呢?

                I have got a for loop where 9 hexagons (hexagon1 through hexagon9) have to be created... But I cannot use hexString as the name of the Sprite because it is a NSString, right ? So how would I make it right ?

                hexString [<- 我希望 for 循环生成hexagon1",然后是hexagon2"等等,而不是 NSString] = [self createHexagon:ccp(xVal,yVal) :i];

                int hexCount = [[[itemPositions valueForKey:myString]valueForKey:@"hexposition"] count];
                
                    for (int i=1;i<=hexCount;i++){
                        NSString *hexString = [NSString stringWithFormat:@"hexagon%d",i];
                        NSNumber *generatedXVal = [[[[itemPositions valueForKey: myString ]valueForKey:@"hexposition"] valueForKey: hexString]valueForKey: @"xVal"];
                        int xVal = [generatedXVal integerValue];
                        NSNumber *generatedYVal = [[[[itemPositions valueForKey: myString ]valueForKey:@"hexposition"] valueForKey: hexString ]valueForKey: @"yVal"];
                        int yVal = [generatedYVal integerValue];
                
                        hexString = [self createHexagon:ccp(xVal,yVal) : i];
                        NSLog(@"%@", hexString);
                    }
                

                推荐答案

                使用 NSMutableDictionary.

                for (int i=1;i<=hexCount;i++){
                    NSString *hexString = [NSString stringWithFormat:@"hexagon%d",i];
                    CCSprite *sprite = [self doSomethingToGetSprite];
                
                    [mutableDictionary setObject:sprite forKey:hexString];
                }        
                

                稍后您可以使用以下方法遍历字典中的所有精灵:

                Later you can iterate over all the sprites in the dictionary using:

                for (NSString *key in mutableDictionary) {
                    CCSprite *sprite = [mutableDictionary objectForKey:key];
                    [self doStuffWithSprite:sprite];
                }
                

                顺便说一句,你为什么要覆盖你在这里分配的 hexString:

                By the way, why are you overwriting hexString that you assign here:

                NSString *hexString = [NSString stringWithFormat:@"hexagon%d",i];
                

                这里有一个:

                hexString = [self createHexagon:ccp(xVal,yVal) : i];
                

                并且该方法调用是一个明显的语法错误,其中悬挂 : i 部分.

                And that method call is an obvious syntax error with the dangling : i part there.

                这篇关于使用 NSString 制作变量名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Hardware Volume buttons change in app volume(硬件音量按钮更改应用程序音量)
                Cocos2d - How to check for Intersection between objects in different layers(Cocos2d - 如何检查不同层中对象之间的交集)
                Highlight Read-Along Text (in a storybook type app for iPhone)(突出显示朗读文本(在 iPhone 的故事书类型应用程序中))
                Cocos2D + Disabling only Retina iPad Graphics(Cocos2D + 仅禁用 Retina iPad 图形)
                How to convert 32 bit PNG to RGB565?(如何将 32 位 PNG 转换为 RGB565?)
                Proper cocos2d scene restart?(正确的 cocos2d 场景重启?)

                    <tfoot id='hIaca'></tfoot>

                      • <bdo id='hIaca'></bdo><ul id='hIaca'></ul>
                          <tbody id='hIaca'></tbody>
                      • <legend id='hIaca'><style id='hIaca'><dir id='hIaca'><q id='hIaca'></q></dir></style></legend>
                        • <i id='hIaca'><tr id='hIaca'><dt id='hIaca'><q id='hIaca'><span id='hIaca'><b id='hIaca'><form id='hIaca'><ins id='hIaca'></ins><ul id='hIaca'></ul><sub id='hIaca'></sub></form><legend id='hIaca'></legend><bdo id='hIaca'><pre id='hIaca'><center id='hIaca'></center></pre></bdo></b><th id='hIaca'></th></span></q></dt></tr></i><div id='hIaca'><tfoot id='hIaca'></tfoot><dl id='hIaca'><fieldset id='hIaca'></fieldset></dl></div>

                        • <small id='hIaca'></small><noframes id='hIaca'>