2012-05-03 15 views
6

ho un blocco in cui io uso self così dichiaro debole riferimento a sé:blocchi nidificati e riferimenti a sé

__weak MyClass *weakSelf = self; 

Ora le mie domande:

  1. ottengo un errore in cui ho define weakSelf e non capisco cosa dovrebbe significare:

    attributo debole non può essere specificato su una variabile automatica

  2. Dentro il mio blocco I passano weakSelf ad un altro blocco e non sono sicuro se ora devo fare la stessa cosa di nuovo in questo modo:

    __weak MyClass *weakWeakSelf = weakSelf; 
    

    E poi passare weakWeakSelf a tale blocco?

+0

Avete già trovato la risposta per questo? La parte weakWeakSelf intendo. –

risposta

8

questo è più probabile avvenendo come ci si rivolge verso il basso per iOS 4. si dovrebbe cambiare per essere

__unsafe_unretained MyClass *weakWeakSelf = weakSelf; 
+0

L'obiettivo è 5.0 ... – Besi

+0

Divertente se cambio il mio 'Obiettivo di distribuzione' su <5.0, ottengo l'errore che avevi. ma sopra questo va bene. Questo ha risolto il problema? –

+0

Devo dichiarare anche weakWeakSelf? – Besi

3

Con ARC

__weak __typeof__(self) wself = self; 

wihtout ARC

__unsafe_unretained __typeof__(self) wself = self; 
1

Con libextobjc sarà leggibile e facile:

- (void)doStuff 
{ 
    @weakify(self); 
    // __weak __typeof__(self) self_weak_ = self; 

    [self doSomeAsyncStuff:^{ 

     @strongify(self); 
     // __strong __typeof__(self) self = self_weak_; 

     // now you don't run the risk of self being deallocated 
     // whilst doing stuff inside this block 
     // But there's a chance that self was already deallocated, so 
     // you could want to check if self == nil 

     [self doSomeAwesomeStuff]; 

     [self doSomeOtherAsyncStuff:^{ 

      @strongify(self); 
      // __strong __typeof__(self) self = self_weak_; 

      // now you don't run the risk of self being deallocated 
      // whilst doing stuff inside this block 
      // Again, there's a chance that self was already deallocated, so 
      // you could want to check if self == nil 

      [self doSomeAwesomeStuff]; 

     }]; 
    }]; 
}