Ios 将ViewController类用作方法中的参数

Ios 将ViewController类用作方法中的参数,ios,viewcontroller,Ios,Viewcontroller,你好。我是iOS新手。我正在创建一个静态库,我有一个接受viewcontroller的方法,所以,我像这样在标题中导入uikit myclass.h #import <UIKit/UIKit.h> 所以我得到一个警告:从不兼容的类型“UIViewController*”分配给“id” 请帮忙。谢谢。首先,从风格上讲,如果变量是viewController,则变量不应是view,但这不会导致错误或警告,只是让阅读代码的人感到困惑 第二,您传入了一个locationManager变量,

你好。我是iOS新手。我正在创建一个静态库,我有一个接受viewcontroller的方法,所以,我像这样在标题中导入uikit

myclass.h

#import <UIKit/UIKit.h>
所以我得到一个警告:从不兼容的类型“UIViewController*”分配给“id”


请帮忙。谢谢。

首先,从风格上讲,如果变量是viewController,则变量不应是
view
,但这不会导致错误或警告,只是让阅读代码的人感到困惑

第二,您传入了一个
locationManager
变量,但过度编写了它,因此它没有任何作用

最后,您收到警告的原因是
CLLocationManager
delegate
属性声明为
id
,但您正在分配UIViewController。现在,可能是您的特定UIViewController实例实现了
CLLocationManagerDelegate
,但编译器不知道这一点,因此会发出警告。如果运行时一切顺利,它将正常工作,但如果有人传递的UIViewController实例未实现
CLLocationManagerDelegate
,则当位置管理器尝试调用委托方法时,可能会出现异常

你应该写-

- (CLLocationManager *) initializeLocationManagerWithDelegate:(id<CLLocationManagerDelegate>) delegate {

   CLLocationManager *manager=[[CLLocationManager alloc]init];
   manager.delegate=delegate;

   return manager;
}
-(CLLocationManager*)初始化LocationManagerWithDelegate:(id)delegate{
CLLocationManager*管理器=[[CLLocationManager alloc]init];
manager.delegate=代表;
退货经理;
}

文件中显示的警告在哪里?是在上面一行吗-(void)initializeLoca。。。。或者它在其他地方?还有,myclass.h是什么类型的对象?
- (CLLocationManager *) initializeLocationManagerWithDelegate:(id<CLLocationManagerDelegate>) delegate {

   CLLocationManager *manager=[[CLLocationManager alloc]init];
   manager.delegate=delegate;

   return manager;
}