27

Ho appena avviato un nuovo progetto con Xcode 4.2.1 e iOS5 SDK. Il progetto è configurato con ARC. Sto cercando di impostare l'AppDelegate essere il delegato per UITabBarController facendo [tabBarController setDelegate:self]; se faccio che ricevo un messaggio di avviso dicendo:Cocoa-Touch - Confusione delegati

warning: Semantic Issue: Sending 'AppDelegate *const __strong' to parameter of incompatible type 'id<UITabBarControllerDelegate>' 

Va bene abbastanza giusto, ho impostato la mia AppDelegate essere conformi al UITabBarControllerDelegate facendo

@interface AppDelegate : UIResponder <UIApplicationDelegate, UITabBarControllerDelegate> 

Ottimo, l'avviso scompare.

Ora ho un altro errore. In un controller della vista che voglio ottenere una sospensione del AppDelegate così faccio questo: AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; ma questo renderà un avviso dicendo:

warning: Semantic Issue: Initializing 'AppDelegate *__strong' with an expression of incompatible type 'id<UIApplicationDelegate>' 

Ma se rimuovo che il mio AppDelegate conforme al protocollo UITabControllerDelegate mio secondo scompare avvertimento .

Comportamento molto strano, cosa offre agli esperti Cocoa?

risposta

81

Provare a fare un typecast prima di assegnare la variabile AppDelegate.

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 

E, mantenere il UITabControllerDelegate.

+1

Naturalmente! Grazie ... –

+2

Ciao @Aadhira, questo ha risolto il mio errore, ma non sono sicuro di aver capito perché la soluzione funziona. Vorresti o qualcuno mi spiegasse? Grazie! – narner

+0

http://stackoverflow.com/questions/14977284/in-objective-c-why-is-casting-needed-when-assigning-to-a-variable-whose-type-is Si prega di fare riferimento al link sopra - @narner – Arundev