Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Incompatible pointer types assigning to 'id<AVAudioPlayerDelegate>' from 'Class'

Tags:

warnings

ios5

I have a "Utility" class that implements the AVAudioPlayerDelegate protocol.

This is my Utility.h

@interface Utility : NSObject <AVAudioPlayerDelegate>
{
}

And this is its counterpart Utility.m

@implementation Utility

static AVAudioPlayer *audioPlayer;

+ (void)playAudioFromFileName:(NSString *)name ofType:(NSString *)type withPlayerFinishCallback:(SEL)callback onObject:(id)callbackObject
{
    ... 
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL: [self getResourceURLForName:name ofType:type] error: nil];
    audioPlayer.delegate = self; // this is the line that causes the Warning
    ...
}

My iOS application works well, however after migrating to iOS 5 and Xcode 4.2 the compiler started throwing this warning, located at the audioPlayer.delegate = self; line:

Incompatible pointer types assigning to id <AVAudioPlayerDelegate> from 'Class'

How can I get rid of it?

like image 990
microspino Avatar asked Oct 29 '11 17:10

microspino


2 Answers

You've declared your method as a class method, and you're trying to use the Class object as the delegate. But you can't add protocols to Class objects.

You need to change playAudioFromFileName:... to an instance method and create an instance of Utility to use as the delegate. Maybe you want to have a single instance of Utility shared by all callers. This is the Singleton pattern, and it's pretty common in Cocoa. You do something like this:

Utility.h

@interface Utility : NSObject <AVAudioPlayerDelegate>
+ (Utility *)sharedUtility;
@end

Utility.m

@implementation Utility

+ (Utility *)sharedUtility
{
    static Utility *theUtility;
    @synchronized(self) {
        if (!theUtility)
            theUtility = [[self alloc] init];
    }
    return theUtility;
}

- (void)playAudioFromFileName:(NSString *)name ofType:(NSString *)type withPlayerFinishCallback:(SEL)callback onObject:(id)callbackObject
{
    ... 
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL: [self getResourceURLForName:name ofType:type] error: nil];
    audioPlayer.delegate = self;
    ...
}

@end

Usage

[[Utility sharedUtility] playAudioFromFileName:@"quack" ofType:"mp3" withPlayerFinishCallback:@selector(doneQuacking:) onObject:duck];
like image 182
rob mayoff Avatar answered Oct 02 '22 20:10

rob mayoff


When you don't need an instance of a Class, just manually get ride of the warning:

audioPlayer.delegate = (id<AVAudioPlayerDelegate>)self;

On the other hand, please note that if you need a Delegate, it means you should have an instance of a Class as a good coding practice instead of a static Class. It can be made a singleton easily:

static id _sharedInstance = nil;
+(instancetype)sharedInstance
{
    static dispatch_once_t p;
    dispatch_once(&p, ^{
        _sharedInstance = [[self alloc] init];
    });
    return _sharedInstance;
}
like image 35
Cœur Avatar answered Oct 02 '22 19:10

Cœur