我收到有关将 *const _strong
传递给类型 id
的语义问题的警告,无论我如何更改似乎都无法修复它.
I'm getting a warning about a semantic issue pertaining to passing a *const _strong
to type id
and cannot seem to fix it no matter what I change.
我目前有两个视图,并且已经编写了这段代码.在 iPadSpeckViewController.m 中,这里是应该在视图之间切换的方法:
I have two views at the moment, and have written this code. In iPadSpeckViewController.m, here is the method that should switch between views:
-(IBAction) touchProducts {
ProductsViewController *controller = [[ProductsViewController alloc]
initWithNibName:@"Products" bundle:nil];
controller.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
controller.delegate = self;
[self presentModalViewController:controller animated:YES];
}
对于 ProductsViewController.h:
And for ProductsViewController.h:
@interface ProductsViewController : UIViewController {
id<ProductsViewControllerDelegate> delegate;
}
@property(nonatomic, retain)
IBOutlet id<ProductsViewControllerDelegate> delegate;
ProductsViewController.m 包含:
ProductsViewController.m contains:
@synthesize delegate;
但是观点并没有改变……有什么想法吗?
But the views do not switch... Thoughts?
这是确切的警告,因为它出现在controller.delegate = self;"行上在 iPadSpeckViewController.m 中:
Here is the exact warning, as it appears on the line "controller.delegate = self;" in iPadSpeckViewController.m:
/Developer/iPadSpeckApp/iPadSpeckApp/iPadSpeckAppViewController.m:17:27:{17:27-17:31}: warning: passing 'iPadSpeckAppViewController *const __strong' to parameter of incompatible type 'id<ProductsViewControllerDelegate>' [3]
这个警告措辞奇怪,但它实际上只是告诉你 self 的类(不管那个类是什么)不符合 ProductsViewControllerDelegate协议.要消除警告,您有两种选择:
This warning is oddly worded, but it is actually just a way of telling you that the class of self (whatever that class is) fails to conform to the ProductsViewControllerDelegate protocol. To get rid of the warning, you have two choices:
在其 @interface
语句中声明 self 的类(无论该类是什么),以符合 ProductsViewControllerDelegate 协议:
Declare the class of self (whatever that class is), in its @interface
statement, to conform to the protocol ProductsViewControllerDelegate:
@interface MyClass : NSObject <ProductsViewControllerDelegate>;
通过改变这个来抑制警告:
Suppress the warning by changing this:
controller.delegate = self;
到这里:
controller.delegate = (id)self;
委托属性的类型为 id
.但自我不是.在 ARC 下,您必须明确强制转换,以便类型正式一致.(我相信这是为了让 ARC 可以绝对确定它有足够的信息来做出正确的内存管理决策.)
The delegate property is typed as id<ProductsViewControllerDelegate>
. But self is not. Under ARC you must make the cast explicit, so that the types formally agree. (I believe this is so that ARC can make absolutely certain it has sufficient information to make correct memory management decisions.)
这篇关于处理应用程序委托和在视图之间切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!