objective c - creating an object and setting a retaining property -
objective c - creating an object and setting a retaining property -
how should set retained property when creating object using alloc , init? (without using autorelease)
with line in header (and corresponding @synthesize line in implementation):
@property(retain)uiwebview *webview;
these 3 options have (i think):
uiwebview *tempwebview = [[uiwebview alloc] init]; [tempwebview setdelegate:self]; tempwebview.hidden = yes; self.webview = tempwebview; [tempwebview release];
(this 1 seems best concerning memory management it's more lines of code , involves stupid variable name, decrease in readability)
self.webview = [[uiwebview alloc] init]; [self.webview release]; [self.webview setdelegate:self]; self.webview.hidden = yes;
(this 1 it's more obvious whats happening memory management doesn't seem great, xcode's analyser doesn't it)
webview = [[uiwebview alloc] init]; [self.webview setdelegate:self]; self.webview.hidden = yes;
(this 1 shortest, it's more obvious first illustration whats happening. bypasses setter, should custom implementation of setter implemented later won't work in case)
so illustration should used, or there improve way?
the best option, imo, 1 don't like, i.e. using autorelease:
self.webview = [[[uiwebview alloc] init] autorelease];
if not want , want one-liner initialization, alternative 3rd one:
webview = [[uiwebview alloc] init];
since others requires explicit line release.
i don't see bad, when belongs init
method , don't reassign elsewhere without using property, , myself utilize when seems reasonable me.
what works retained properties convenience constructors like:
self.image = [uiimage imagewithcontentsoffile:xxxxxxx];
so, perchance if find none of options listed fine you, might add together category uiwebview
, convenience constructor doing autorelease job you:
self.webview = [uiwebview webviewwith......];
objective-c memory-management properties retain
Comments
Post a Comment