我在应用程序委托(delegate)中有一段工作代码,其中包含对 View 的引用,该 View 可能会或可能不会在应用程序的使用过程中创建。这段代码最终只会在引用的对象存在后被调用,所以不会有崩溃的可能。
但是,编译器检测到这个引用并给出警告:
warning: Semantic Issue: Instance method
'-dismissPurchasingViewAndUpdateSetupView' not found (return type
defaults to 'id')
简而言之,这就是正在发生的事情:
在应用委托(delegate)中:
@interface appDelegate : NSObject
{
NSObject *purchasingView;
}
@property (nonatomic, retain) NSObject *purchasingView;
@end
@implementation appDelegate
@synthesize purchasingView;
-(void)aMethod
{
[purchasingView dismissPurchasingViewAndUpdateSetupView];
}
在 View Controller 中:
-(void) someOtherMethod
{
//Let the app delegate know about the reference to this view
appDelegate *appDelegate = (appDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.purchasingView = self;
}
-(void)dismissPurchasingViewAndUpdateSetupView
{
[self dismissModalViewControllerAnimated:YES];
}
app delegate 实现了一个 SKPaymentTransactionObserver
并且它在 View Controller 中接收由购买触发的 App Store 通知。当购买完成时,应用程序委托(delegate)通知 View Controller 自行关闭。
这样做是否明智?有没有办法让编译器忽略应用首次运行时 purchasingView
指针指向 null 的事实?
最佳答案
您没有收到错误,因为当应用程序启动时 purchasingView
可能是 nil
。
你收到错误是因为 purchasingView
是一个 NSObject
,而 NSObject
没有实现 -dismissPurchasingViewAndUpdateSetupView
方法。如果您将 purchasingView
的类型更改为您实现 purchasingView
的任何类,那么错误就会消失。
关于ios - 引用一个尚不存在的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9374741/