how to use pointers in Objective c

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

Answers


- (void)setupWebView:(UIWebView**)aWebView {

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 get

That method should simply return the created instance directly. And this just makes it worse -- is flat out wrong:

 if (*aWebView) {
    [*aWebView release];
 }

 *aWebView = webview;
  1. 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).

  2. it'll crash if aWebView is NULL; crash on the assignment, specifically.

  3. if aWebView refers to an iVar slot, it bypasses any possible property use (a different way of breaking encapsulation).


It is a method to initialize a pointer. The first line allocates the object. The if statement makes sure that the passed in pointer-to-a-pointer is not already allocated, if it is it releases it. then it sets the referenced pointer to the newly allocated object.


The answer by @bbum is probably correct, but leaves out one aspect to the question that I see there. There are many examples in Foundation which use pointer-pointers in the method signature, so you can say it is a common pattern. And those are probably not a beginners mistake.

Most of these examples are similar in that they fall into one category: the API tries to avoid the usages of exceptions, and instead use NSError for failures. But because the return value is used for a BOOL that signals success, an NSError pointer-pointer is used as output parameter. Only in the probably rare error case an NSError object is created, which can contain error code and error descriptions, and localized descriptions and possibly even more information (like an array of multiple errors in the case of bulk operations). So the main success case is efficient, and the error case has some power to communicate what went wrong, without resorting to exceptions. That is the justification behind these signatures as I understand it.

You can find examples of this usage in both NSFileManager and NSManagedObjectContext.

One might be tempted to use pointer-pointers in other cases where you want multiple return values and an array does not make sense (e.g. because the values are not of same type), but as @bbum said, it is likely better to look hard for alternatives.


Need Your Help

How to add multiple controller in single page in AngularJS

javascript html angularjs

I am new to AngularJS I have a problem with this code. I want to add multiple controller in single ng-app. But it execute first one. Why not second one?

WPF DataGrid extended "copy and paste"

wpf datagrid copy paste

How can I make the WPF DataGrid have some sort of improved "copy and paste" where I can select a single cell , copy using Ctrl-C , select a bunch of cells of columns and paste using Ctrl-V ???