美文网首页
Incompatible pointer types assi

Incompatible pointer types assi

作者: goyohol | 来源:发表于2017-11-16 17:20 被阅读589次


写一个判断rootviewcontroller的工具类CheckRootVC_Obj,想使用类方法,又在此类方法里面使用了代理

发现:使用了类方法,并想在该方法里delegate的时候,就出现警告。
解决:用一个实例(对象)方法 + 单例模式。


例子🌰:

在“- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { }”里:

//判断 初始VC
[CheckRootVC_Obj checkRootVC]; //类方法


判断类CheckRootVC_Obj

CheckRootVC_Obj.h:

@interface CheckRootVC_Obj : NSObject <UITabBarControllerDelegate>

+(void)checkRootVC;//监测 初始VC

@end


CheckRootVC_Obj.m:

+(void)checkRootVC { //监测 初始VC

  AppDelegate * appDel = (AppDelegate *)[UIApplication sharedApplication].delegate;

  bool hasUsedApp = [User_Def boolForKey:HAS_UsedApp];

  if (hasUsedApp == YES) {  
     //�选择 主界面
     RootTabBarViewController * rootTabBarVC = [[RootTabBarViewController alloc] init];
     rootTabBarVC.delegate = self;
     appDel.window.rootViewController = rootTabBarVC;
   
   } else {    
      // 引导页
      GuideViewController * guideVC = [[GuideViewController alloc] init];
      appDel.window.rootViewController = guideVC;        
   }


}


出现警告:




解决: 单例类的实例对象 执行 对象方法

在“- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { }”里:

//判断 初始VC
[[CheckRootVC_Obj sharedCheckRootVC_Obj] checkRootVC];


判断类CheckRootVC_Obj 改为 单例类!

CheckRootVC_Obj.h:

@interface CheckRootVC_Obj : NSObject <UITabBarControllerDelegate>

+(CheckRootVC_Obj *)sharedCheckRootVC_Obj;//单例 实例化方法

-(void)checkRootVC;//监测 初始VC

@end

CheckRootVC_Obj.m:

+(CheckRootVC_Obj *)sharedCheckRootVC_Obj {//单例 实例化方法
   static CheckRootVC_Obj *theCheckRootVC_Obj;
   @synchronized(self) {
       if (!theCheckRootVC_Obj)
           theCheckRootVC_Obj = [[self alloc] init];
   }
   return theCheckRootVC_Obj;
}


-(void)checkRootVC { //监测 初始VC

   AppDelegate * appDel = (AppDelegate *)[UIApplication sharedApplication].delegate;

   bool hasUsedApp = [User_Def boolForKey:HAS_UsedApp];

   if (hasUsedApp == YES) {  
      //�选择 主界面
      RootTabBarViewController * rootTabBarVC = [[RootTabBarViewController alloc] init];
      rootTabBarVC.delegate = self;
      appDel.window.rootViewController = rootTabBarVC;

   } else {    
       // 引导页
       GuideViewController * guideVC = [[GuideViewController alloc] init];
       appDel.window.rootViewController = guideVC;        
   }

}


无警告:

屏幕快照 2017-11-16 下午5.06.55.png









参考StackOverflow论坛的文章内容:
Incompatible pointer types assigning to 'id<AVAudioPlayerDelegate>' from 'Class'

stackoverflow logo

自定义的Utility类

Utility.h:

@interface Utility : NSObject <AVAudioPlayerDelegate>

+ (void)playAudioFromFileName:(NSString *)name ofType:(NSString *)type withPlayerFinishCallback:(SEL)callback onObject:(id)callbackObject;
@end

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; //这行会产生警告⚠️⚠️

}
警告:

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

分析:

声明的方法作为该类的类方法,并且想让这个对象 使用代理,却不能添加协议类对象。


解决:

需要更改[playAudioFromFileName: ofType: withPlayerFinishCallback: onObject: ]实例(对象)方法,并创建一个该类的实例方法作为代理。
也许你想要一个实用共享于所有调用者的实例,可使用单例模式


单例类解决:
Utility.h:

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

- (void)playAudioFromFileName:(NSString *)name ofType:(NSString *)type withPlayerFinishCallback:(SEL)callback onObject:(id)callbackObject;
@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



最终:使用单例类调用方法

[[Utility sharedUtility] playAudioFromFileName:@"Free Loop" ofType:"mp3" \
withPlayerFinishCallback:@selector(doneFinishNotice:) onObject:daniel];



笔记做在那里很久了,可是自己写的代码找了好久。。
StackOverflow论坛的大牛们 是厉害!!😂😂😂😂😂









goyohol's essay

相关文章

网友评论

      本文标题: Incompatible pointer types assi

      本文链接:https://www.haomeiwen.com/subject/dciplxtx.html