我有一个 ”Utility“ 实现的类AVAudioPlayerDelegate协议。
这是我的实用程序.h
@interface Utility : NSObject <AVAudioPlayerDelegate>
{
}
这是它的对应物实用程序.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;
line:
Incompatible pointer types assigning to id <AVAudioPlayerDelegate> from 'Class'
我怎样才能摆脱它?
您已将方法声明为类方法,并且尝试使用 Class 对象作为委托。但是您不能将协议添加到 Class 对象。
你需要改变playAudioFromFileName:...
到一个实例方法并创建一个实例Utility
用作代表。也许您想要一个实例Utility
由所有呼叫者共享。这就是 Singleton 模式,在 Cocoa 中很常见。你做这样的事情:
实用程序.h
@interface Utility : NSObject <AVAudioPlayerDelegate>
+ (Utility *)sharedUtility;
@end
实用程序.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];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)