Objective-C类别和新的iVar [英] Objective-C Category and new iVar

查看:137
本文介绍了Objective-C类别和新的iVar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试扩展cocos2d的SimpleAudioEngine的功能,能够连续播放几个声音效果作为某种链。我试图这样做与扩展。然而,我现在意识到,我可能还需要一个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.

然而,似乎我不能在类别中添加iVars。相反,我试图使用一个扩展,但似乎他们需要在类的原始.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

我试图定义iVars作为属性,这将编译。但我不能综合它们,也没有任何其他可能性来绑定到任何方法。

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];

如果有另一种方式产生相同/类似的结果,我也会非常感谢。 / p>

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆