Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 将代理设置为“0”;“自我”;从类方法_Iphone_Objective C_Ios_Class_Delegates - Fatal编程技术网

Iphone 将代理设置为“0”;“自我”;从类方法

Iphone 将代理设置为“0”;“自我”;从类方法,iphone,objective-c,ios,class,delegates,Iphone,Objective C,Ios,Class,Delegates,比方说,我想从助手类方法调用UIActionSheet。我希望helper类(而不是对象)是这个actionsheet的委托。 所以我要把自己交给代表 UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"MyTitle" delegate:self

比方说,我想从助手类方法调用UIActionSheet。我希望helper类(而不是对象)是这个actionsheet的委托。 所以我要把自己交给代表

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"MyTitle"
                                                         delegate:self
                                                cancelButtonTitle:nil
                                           destructiveButtonTitle:@"Delete" 
                                                otherButtonTitles:nil];
我的helper类将委托方法实现为类方法,一切正常。 但是,我从编译器那里得到一个警告,它说,指针不兼容,在需要id时发送类。我也尝试了[自我课堂],得到了同样的警告


如何避免此警告?

在将对象指针传递给该方法之前,您不会看到此警告。它需要一个
id
,其中您给出
Class
,它是一个
typedef-struct-objc_-Class*类

您可以通过将self强制转换为type id来消除警告

[[UIActionSheet alloc] initWithTitle:@"MyTitle"
                            delegate:(id<UIActionSheetDelegate>)self
                   cancelButtonTitle:nil
              destructiveButtonTitle:@"Delete" 
                   otherButtonTitles:nil];
[[UIActionSheet alloc]initWithTitle:@“MyTitle”
代表:(id)self
取消按钮:无
破坏性按钮:@“删除”
其他按钮:无];

这将告诉编译器将该值视为符合UIActionSheetDelegate协议的
id

只需将委托设置为
[self-self]
,我通过在Objective-C中的class.h文件接口声明中添加来解决它,这些被称为“类方法”,而不是“静态方法”。这可能有助于您在以后的搜索中获得帮助。:)谢谢。我对Objective-C和C#进行编码切换。无法避免拼写错误。谢谢。我对Objective-C和C#进行编码切换。无法避免拼写错误。我只想说“不要这样做。”“。这会让任何阅读你的代码的人都感到困惑。”。每当我认为在类方法中放入大量代码是个好主意时,我发现自己在一周后改变了主意,创建了一个Singleton对象。如果其他人接管了您的项目,他们会在这里发帖,询问如何使用类作为委托。实际上我并不完全确定,但由于
self
是指向将接收消息的当前对象的指针,因此将
class
对象屏蔽为
self
指针会让编译器感到轻松。不过,这只是一个编译器的东西;使用
self
可以很好地工作,但是编译器警告会让你发疯。“谢谢。它可以工作,但是它是如何工作的?”->这是一个优秀程序员的可靠标志。无论如何,在SWIFT中这样做?