I have seen some iOS developpers using code like this :
- (void)setupWebView:(UIWebView**)aWebView {
UIWebView *webview = [[UIWebView alloc] init];
.....
if (*aWebView) {
[*aWebView release];
}
*aWebView = webview;
}
Do you know what’is this mean and why we use this ? thanks
That is awful. You should never have a method that returns void, but sets an argument by reference unless:
• there are multiple arguments set
• the method is prefixed with
getThat method should simply return the created instance directly. And this just makes it worse — is flat out wrong:
it breaks encapsulation; what if the caller passed a reference to an iVar slot. Now you have the callee managing the callers memory which is both horrible practice and quite likely crashy (in the face of concurrency, for example).
it’ll crash if
aWebViewis NULL; crash on the assignment, specifically.if
aWebViewrefers to an iVar slot, it bypasses any possible property use (a different way of breaking encapsulation).