我有一个实现AVAudioPlayerDelegate协议的“实用程序”类。
这是我的Utility.h
@interface Utility : NSObject <AVAudioPlayerDelegate>
{
}这是对应的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
...
}我的iOS应用程序运行良好,但是在迁移到iOS 5和Xcode 4.2之后,编译器开始抛出这个警告,位于audioPlayer.delegate = self;行:
Incompatible pointer types assigning to id <AVAudioPlayerDelegate> from 'Class'我怎么才能摆脱它?
发布于 2013-08-19 14:02:34
当您不需要类的实例时,只需手动获得警告:
audioPlayer.delegate = (id<AVAudioPlayerDelegate>)self;另一方面,请注意,如果您需要一个委托,这意味着您应该有一个类的实例作为一种良好的编码实践,而不是静态类。它可以很容易地成为一个辛格尔顿:
static id _sharedInstance = nil;
+(instancetype)sharedInstance
{
static dispatch_once_t p;
dispatch_once(&p, ^{
_sharedInstance = [[self alloc] init];
});
return _sharedInstance;
}https://stackoverflow.com/questions/7940477
复制相似问题