Using 'self' in an @implementation

≡放荡痞女 提交于 2019-12-10 18:46:54

问题


I'm trying to play voice audio on top of the iPodMusicPlayer by making a TriggerIO native plugin, however I'm having trouble accessing the self object.

#import "alert_API.h"

@implementation alert_API

+ (void)play:(ForgeTask*)task text:(NSString *)filename {
    NSURL* url = [[NSBundle mainBundle] URLForResource:@"Rondo_Alla_Turka_Short" withExtension:@"aiff"];
    NSAssert(url, @"URL is valid.");
    NSError* error = nil;


    /* ERROR: /Users/gsquare567/forge-workspace/plugins/audio/inspector/ios-inspector/ForgeModule/alert/alert_API.m:45:13: Member reference type 'struct objc_class *' is a pointer; maybe you meant to use '->'? */
    self->player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];

    /* ERROR: /Users/gsquare567/forge-workspace/plugins/audio/inspector/ios-inspector/ForgeModule/alert/alert_API.m:45:13: Incomplete definition of type 'struct objc_class' */
    if(!self.player)
    {
        NSLog(@"Error creating player: %@", error);
    }


    [task success:nil];
}

@end

The property is defined in alert_API.h:

#import <Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h>

@interface alert_API : NSObject

@property (nonatomic, strong) AVAudioPlayer* player;
+ (void)play:(ForgeTask*)task text:(NSString *)filename;

@end

What do I need to do here to be able to access the player property throughout my API?

Thanks!


回答1:


Your play:text: method is static, meaning self doesn't refer to an instance with the property, but to the class object for alert_API. You can either change your method to be an instance method (- (void) instead of + (void)):

- (void)play:(ForgeTask*)task text:(NSString *)filename;

Or if you want to keep the method static, you will have to implement a static method which returns a player singleton:

+ (AVAudioPlayer *)playerInstance;

Then use [alert_API playerInstance] to access the player from your methods.



来源:https://stackoverflow.com/questions/15951441/using-self-in-an-implementation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!