2012-02-21 39 views
0

我在應用程序委託中包含對應用程序使用過程中可能或可能不會創建的視圖的引用的工作片段。這個代碼只有在引用的對象存在後纔會被調用,所以不會崩潰。引用還不存在的對象

但是,編譯器檢測該基準,給予警告:

warning: Semantic Issue: Instance method 
'-dismissPurchasingViewAndUpdateSetupView' not found (return type 
defaults to 'id') 

簡單地說,這是發生了什麼事:

在應用程序委託:

@interface appDelegate : NSObject 
    { 
    NSObject *purchasingView; 
    } 

    @property (nonatomic, retain) NSObject *purchasingView; 
@end 

@implementation appDelegate 
    @synthesize purchasingView; 

-(void)aMethod 
{ 
    [purchasingView dismissPurchasingViewAndUpdateSetupView];  
} 

在視圖控制器:

-(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]; 
} 

應用程序委託實現了SKPaymentTransactionObserver,它接收由視圖控制器中的購買觸發的App Store通知。購買完成後,應用程序委託人通知視圖控制器自行解除它。

這是一個明智的方式來做到這一點?有沒有辦法讓編譯器忽略這樣的事實,即當應用程序第一次運行時,purchasingView指針指向空?

回答

1

您沒有收到錯誤,因爲purchasingView在應用程序啓動時可能是nil

由於purchasingViewNSObject,並且NSObject未實現-dismissPurchasingViewAndUpdateSetupView方法,所以發生此錯誤。如果您將purchasingView的類型更改爲您實施purchasingView的類別,那麼錯誤將消失。

1

編譯器是而不是在第一次運行時檢測到有關purchasingViewnil的任何內容。它不這樣做。編譯器不關心變量在運行時的值。而且,無論如何,它都是非常有效的消息nil,它是Objective-C運行時非常好用的「特性」。

你的問題是,在應用程序委託你大概不#import -ing頭不管是什麼類的purchasingView是的,你是說purchasingViewappDelegateNSObject。將其更改爲purchasingView#import應用程序委託標題中的標題的實際類別。

0

[purchasingView perfromSelector:@selector(dismissPurchasingViewAndUpdateSetupView)]

相關問題