2015-10-11 11 views
15

Sto provando a convertire il mio Objective-C in Swift - un po 'confuso sull'errore qui e su come gestirlo. Ho letto la documentazione ma sono ancora confusa: è stata generata da un convertitore. Qualcuno ha qualche idea?Identificatore previsto nella dichiarazione di funzione - Obiettivo da C a Swift

Objective-C 
- (id) init 
{ 
    self = [super init]; 

    if (!self) 
     return nil; 

    self.cmRequestsQuery = [[NSMutableArray alloc] initWithCapacity:5]; 
    self.cmQueryIsRuning = NO; 
    self.requestCounter = 0; 
    self.serverOfflineOrBadResponse = NO; 
    self.userWasLoggedIn = NO; 
    self.needToSendPushNotiToken = NO; 
    self.noInternetConection = NO; 
    self.needToUpdateToken = NO; 

    [[reqOperationManager sharedManager] setDelegate:self]; 

    return self; 
} 


Swift 
func init() -> AnyObject { 
    self = super() 
    if !self { 
     return nil 
    } 
    self.cmRequestsQuery = NSMutableArray(capacity: 5) 
    self.cmQueryIsRuning = false 
    self.requestCounter = 0 
    self.serverOfflineOrBadResponse = false 
    self.userWasLoggedIn = false 
    self.needToSendPushNotiToken = false 
    self.noInternetConection = false 
    self.needToUpdateToken = false 
    reqOperationManager.sharedManager().setDelegate(self) 
    return self 
} 
+1

provare: super.init(); – eliasRuizHz

+0

Che si è preso cura di un problema, si lamenta ancora a 'func init() -> AnyObject {' with "Identificatore previsto nella dichiarazione di funzione.Rimozione della parte" func "quindi porta l'errore" Le dichiarazioni consecutive su una riga devono essere separate da ';'. Credo che sia dall'aspetto AnyObject. – user2836292

+0

Vedere [Questo collegamento] (http://stackoverflow.com/questions/24302288/how-to-write-init-method-in-swift) – eliasRuizHz

risposta

27

In Swift init metodi non hanno func parola chiave e nessun valore di ritorno e il punto di chiamare super-è diverso.

init() { 

Prima inizializzare tutte le variabili di istanza.

self.cmRequestsQuery = NSMutableArray(capacity: 5) 
self.cmQueryIsRuning = false 
self.requestCounter = 0 
self.serverOfflineOrBadResponse = false 
self.userWasLoggedIn = false 
self.needToSendPushNotiToken = false 
self.noInternetConection = false 
self.needToUpdateToken = false 

Quindi chiamare super - se necessario - per ottenere l'istanza.

super.init() 

quindi chiamare i metodi che utilizzano self

reqOperationManager.sharedManager().setDelegate(self) 

Questo è tutto.

} 

In alcuni casi si deve aggiungere la parola chiave override prima init().

Per ulteriori dettagli si prega di leggere il capitolo sull'inizializzazione nella Guida Swift. Ne vale la pena.

+0

"Inizializzazione nella guida rapida alla lingua" menzionato alla fine della risposta : https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Initialization.html – AJP

0

Ho avuto questo problema perché stavo cercando di importare l'intestazione rapida (importare "ProjectName-swift.h") nella mia classe swift.

Problemi correlati