ios - Capture the attribute strongly in the block, am converting the app non arc to arc -


this question has answer here:

i have declared variables subclass of uiviewextention. converting project non-arc arc

@interface sectioniconview : uiviewextention  @property (nonatomic,weak)  uiimageview *sectionportraitimageview; @property (nonatomic,weak)  uiimageview *sectionlandscapeimageview; 

i have declared property weak, shows error in .m file i.e.,

assigning retained object weak variable;object released after assignment.

i have changed attribute strong..

@property (nonatomic,strong)  uiimageview *sectionportraitimageview; 

then shows error is:

capture in block lead retain cycle.

how avoid error?

please see this apple documentation on how avoid capturing "self" in block. here's key part:

xyzblockkeeper * __weak weakself = self; self.block = ^{     [weakself dosomething];   // capture weak reference                               // avoid reference cycle } 

if need access ivars within block need following additional pattern

self.block = ^{     xyzblockkeeper * strongself = weakself;     // can access strongself->myivar safely } 

you might think use weakself->myivar lead yet warning race condition. above code ensure self stays alive while block runs, may desirable if aren't accessing ivars.

finally, if code make possible might consider not capturing self @ all, capturing need, avoids lot of complexity:

myclass *mything = self.mything; self.block = ^{     // use mything here, self not captured } 

Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -