Objective-C Category and new iVar(Objective-C 类别和新的 iVar)
问题描述
我尝试扩展 cocos2d 的 SimpleAudioEngine 的功能,使其能够像某种链条一样依次播放多个音效.我试图通过扩展来做到这一点.但是我现在意识到,我可能还需要一个 iVar 来记住所有声音文件的名称,并需要一个 iVar 来记住当前正在播放的声音.
I try to extend the functionality of SimpleAudioEngine of cocos2d with the ability to play several sound effect one after another as some kind of chain. I tried to do this with an extension. However I now realized that I probably also need an iVar to remember the names of all sound files and one to remember which sound is currently playing.
但是,我似乎无法在类别中添加 iVar.相反,我尝试使用扩展名,但似乎它们需要在类的原始 .m 文件中,这样也不起作用.还有其他方法可以让我这样做吗?
However it seems that I cannot add iVars in a category. Instead I tried to use an extension, but it seems that they need to be in the original .m file of the class so that also would not work. Is there yet another way, that allows me to do this?
带有类别的标题
#import <Foundation/Foundation.h>
@interface SimpleAudioEngine(SoundChainHelper)<CDLongAudioSourceDelegate>
-(void)playSoundChainWithFileNames:(NSString*) filename, ...;
@end
以及带有扩展名的 .m 文件:
And the .m-file with the extension:
#import "SoundChainHelper.h"
@interface SimpleAudioEngine() {
NSMutableArray* soundsInChain;
int currentSound;
}
@end
@implementation SimpleAudioEngine(SoundChainHelper)
// read in all filenames and start off playing process
-(void)playSoundChainWithFileNames:(NSString*) filename, ... {
soundsInChain = [[NSMutableArray alloc] initWithCapacity:5];
va_list params;
va_start(params,filename);
while (filename) {
[soundsInChain addObject:filename];
filename = va_arg(params, NSString*);
}
va_end(params);
currentSound = 0;
[self cdAudioSourceDidFinishPlaying:nil];
}
// play first file, this will also always automatically be called as soon as the previous sound has finished playing
-(void)cdAudioSourceDidFinishPlaying:(CDLongAudioSource *)audioSource {
if ([soundsInChain count] > currentSound) {
CDLongAudioSource* mySound = [[CDAudioManager sharedManager] audioSourceForChannel:kASC_Right];
[mySound load:[soundsInChain objectAtIndex:0]];
mySound.delegate = self;
[mySound play];
currentSound++;
}
}
@end
另外,我尝试将 iVar 定义为可以编译的属性.但是我既不能合成它们,也没有任何其他可能将它们绑定到任何方法.
Alternatively I tried to define the iVars as properties, which will compile. However I can neither synthesize them nor do I have any other possibility to bind them to any method.
我尝试将功能实现为 SimpleAudioEngine 的一个类别,这样我只需要记住一个处理我所有声音问题的类.这样我就可以像这样简单地创建一个链:
I try to implement the functionality as a category of SimpleAudioEngine so that I only need to remember one class that deals with all my sound issues. and so that I can create a chain as simple as this:
[[SimpleAudioEngine sharedEngine] playSoundChainWithFileNames:@"6a_loose1D.mp3", @"6a_loose2D.mp3", @"6a_loose3D.mp3", @"6a_loose4D.mp3", @"6b_won1D.mp3", nil];
如果有其他方法可以产生相同/相似的结果,我也将非常感谢.
If there is another way that yields the same/ a similar result I would also be very thankful.
推荐答案
您是正确的,您不能将实例变量(或合成的@properties)添加到类别中.您可以使用 Objective-C 运行时对 关联引用
You are correct that you can't add instance variables (or synthesized @properties) to a category. You can workaround this limitation using the Objective-C runtime's support for Associative References
类似这样的:
在您的 .h 中:
@interface SimpleAudioEngine (SoundChainHelper)
@property (nonatomic, retain) NSMutableArray *soundsInChain;
@end
在你的 .m 中:
#import <objc/runtime.h>
static char soundsInChainKey;
@implementation SimpleAudioEngine (SoundChainHelper)
- (NSMutableArray *)soundsInChain
{
return objc_getAssociatedObject(self, &soundsInChainKey);
}
- (void)setSoundsInChain:(NSMutableArray *)array
{
objc_setAssociatedObject(self, &soundsInChainKey, array, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
@end
(适用标准免责声明.我在浏览器中输入了这个,并没有测试它,但我以前使用过这种技术.)
(The standard disclaimer applies. I typed this in the browser, and didn't test it, but I have used this technique before.)
我链接到的文档包含有关关联引用如何工作的更多信息.
The documentation I linked to has a lot more information about how associative references work.
这篇关于Objective-C 类别和新的 iVar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Objective-C 类别和新的 iVar
基础教程推荐
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01