Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/44.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 轻松演奏_Iphone_Objective C_Ios_Xcode - Fatal编程技术网

Iphone 轻松演奏

Iphone 轻松演奏,iphone,objective-c,ios,xcode,Iphone,Objective C,Ios,Xcode,我有一个很长的代码,这主要是问题所在,哈哈。我有一个很长的代码,我把单词相互比较,当图像被设置到UIImageview中时,我播放声音。问题是对于其中一个,我有5556个选项可以播放,所以我必须输入播放声音代码,我得到5556次,这真的会让我的代码变长(如果你问我的话,已经是这样了) 这是密码 //this is no problem here are only 36 of them. if (aChar1 == aChar7){ [imageview7 setImage:imagev

我有一个很长的代码,这主要是问题所在,哈哈。我有一个很长的代码,我把单词相互比较,当图像被设置到UIImageview中时,我播放声音。问题是对于其中一个,我有5556个选项可以播放,所以我必须输入播放声音代码,我得到5556次,这真的会让我的代码变长(如果你问我的话,已经是这样了)

这是密码

//this is no problem here are only 36 of them. 
if (aChar1 == aChar7){
    [imageview7 setImage:imageview1HG.image];
    [imageviewhidden6 setImage:imageview1H.image];
    {
//soundcode
        CFBundleRef mainBundle = CFBundleGetMainBundle();
        CFURLRef soundFileURLRef;
        soundFileURLRef =CFBundleCopyResourceURL(mainBundle, 
                                                 (CFStringRef) @"beep-7", CFSTR ("wav"), NULL);

        UInt32 soundID;
        AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
        AudioServicesPlaySystemSound(soundID);
    }

}
else if 
    (aChar1 == aChar7 && aChar2 == aChar8 && aChar3 != aChar9 && aChar4 != aChar10 && aChar5 != aChar11 &&  aChar6 != aChar12){
        //for these there are 5556 of these lines and I don't want to put that sound file in all of them any other way to do so? 
        if (aChar1 == aChar9){
            [imageview7 setImage:imageview1HO.image];
        }
        else if (aChar1 == aChar10){
            [imageview7 setImage:imageview1HO.image];
        }
        else if (aChar1 == aChar11){
            [imageview7 setImage:imageview1HO.image];                
        }
        else if (aChar1 == aChar12){
            [imageview7 setImage:imageview1HO.image];
        }
        else {
            [imageview7 setImage:imageview1H.image];}
    }
对于第一个使用imageview1HG设置图像视图的场景,没有问题,其中有36个这样就没有问题了,但是对于使用不同1HO.image的场景,有5556个这样的场景,是否有一些1行代码可以像上面那样播放声音


我在代码中添加了注释。

如果只添加了例如[playsound:beep-7.wav]的5556行,那么添加5556行是没有问题的;但这段声音代码非常长,如果没有其他人建议更好的方法,并且你最终编程了5000多个案例,我肯定会使用Switch而不是if,那么将它们放入这么多次将使它变得非常长。IF将遍历并检查列表中的每个选项,开关将跳转到正确的选项。有了5000多件物品,它将在性能方面产生差异。好吧,你不需要一个
开关来实现你所建议的@Shenaniganz。选择条件后,可以暂停。(我不是说这是一个好的解决方案,只是说有一个替代方案…)我会使用一个像谢南甘茨说的开关。然后,不要添加播放声音5556次,只需等待直到结束,然后检查
imageview7.image
等于什么。我真的不明白你想要实现什么,但我假设每个不同的图像都有不同的声音,所以请检查你指定的图像,然后播放特定的声音,而不是将声音放在每个if(或者更好的切换)案例中。比如if imageview7==imageview1HO.image播放声音?你喜欢那样的工作吗?我可以比较图像视图的内部吗?