3

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!

Garrett
  • 11,451
  • 19
  • 85
  • 126
  • 3
    This is a class method, not an instance method. In a class method, `self` refers to the `class`, not any instance. You can't access ivars from a class method. In a class method, `self` can be used to call other class methods, but that's basically it. – rmaddy Apr 11 '13 at 14:32

1 Answers1

6

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.

Mike Weller
  • 45,401
  • 15
  • 131
  • 151
  • 5
    The `+` means class method, not static method. – rmaddy Apr 11 '13 at 14:34
  • 4
    @rmaddy +1 also `self is not valid in that context.` It is valid. It's a reference to the class object rather than an instance of the class. – Jack Lawrence Apr 11 '13 at 14:37
  • Thanks a lot! The `-` instance method compiles, but to work as a plugin method I need to keep it as a class method. I just learned a lot :) – Garrett Apr 11 '13 at 14:44
  • @rmaddy Just out of curiosity: Can you please provide an example *static method* which can't be called *class method* – viral Apr 12 '13 at 07:31
  • 1
    @Viral - Objective-C doesn't have static methods. You can define static functions in a source file but these aren't methods. – rmaddy Apr 12 '13 at 18:09