0

Just new to objective-C. I understand that a pointer should be used when creating a new object, e.g.

class *var = .... 

Now, I also understand that when calling this object, let's say in a method, once it has already been created, we do not have to use the pointer, correct?

In what other instance(s), outside of object creation, should we use the * mark?

2
  • You mean other than its use in C code (which is legal in Objective-C)? Commented Oct 27, 2014 at 15:09
  • 1) Yes, I meant when to use the asterisk. Sorry that I wasn't clear. I understand that the pointer should always be used, but what I do not understand is when to use the asterisk, but now I have somewhat of an idea. 2) I would give your answer a vote up, but I am not able to :( Commented Oct 27, 2014 at 15:27

2 Answers 2

1

You always use pointers, because OOP-objects are allocated on heap and in C C-objects (what C calls an object, everything "living" at runtime and having an address including OOP-objects) with the storage class allocated are always referred using a pointer. For OOP-objects this is a pointer to an object, very often called object reference.

In detail:

NSString *pointer = [NSString new]; // pointer is a pointer to an object, an object reference … [pointer doSomething]; // pointer *is* a pointer, so a pointer *is* used. 

Probably you want to ask, when to use the asterisk * for object references as in NSString*? Only in a type definition (including casting), because in an expression it would mean dereference the pointer. The result would be an OOP-object itself (not its reference). But you always use the pointer for OOP-objects.

[*pointer doSomething]; *pointer is an object. But you address receiver of messages using a pointer, not an object 
Sign up to request clarification or add additional context in comments.

5 Comments

And of course the common use of setting NSError via its (NSError **)error parameter.
If a var error has the type NSError**, than error is not an object pointer. Therefore it is simple C. And in C you use * very often – of course. The above answer is only related to (OOP) object pointers and (OOP) objects as the Q is. But I will clarify it.
Thank-you very much for this. Perfect explanation for my novice, objC brain.
When you use a referencing type of your variable you are basically do the composition (compose many classes in another one). So you can see your NSStrings like instances of NSString class. You are telling to your program something like "Hey this variable is NSString, you probably dont know the NSString data type so here(pointing) you can find it".
I do not think, that the comment is related to my A, but to the Q. However, in Objective-C you always use a pointer var, so it is not a question of composing or aggregating. furthermore you use the type for local vars, which typically neither compose nor aggregate something.
1

Yes, you'll use * for pointers to objects. But there are other, less common uses of pointers:

  1. For example, consider the enumerateWithBlock method of NSArray in which the block will pass you the address to some BOOL variable that you can update. You'd use * to update whatever the pointer points to:

    NSArray *array = @[ ... ]; // define the array any way you want [array enumerateWithBlock:^(id obj, NSUInteger idx, BOOL *stop)) { // do something with each object in the array // if you want to stop (i.e. you found what you need), you'd do something like: if (...) { *stop = YES; } }]; 

    This pattern is used in a lot of enumeration methods.

  2. A variation of the above pattern is the typical Objective-C error handling pattern. For example, you may want to return a particular value upon success, but upon error, pass back the details of that error, too:

    /* Return NSData. Upon error, return nil, but also provide `NSError` object if we were * provided a pointer to a NSError pointer. */ - (NSData *)someMethodWithParameter:(NSString *)searchTerm error:(NSError * __autoreleasing *)error { // do something with searchTerm // but, if there was a problem, update error (if we were provided error pointer): if (successful) { return data; } else { if (error) { *error = [NSError errorWithDomain:kSomeErrorDomain code:kSomeErrorCode userInfo:nil]; } return nil; } } 

    and you'd use that like:

    NSError *error; NSData *data = [self someMethodWithParameter:searchTerm error:&error]; if (!data) { // handle the error here } 

    For more information, see Error Handling Programming Guide: Using and Creating Error Objects.

2 Comments

Correct, it's a pointer to a pointer to an object. That's my point. The OP asked "When to use a pointer in obj-C?" and I wanted to provide two common pointer patterns that you see in Objective-C code.
Extremely useful, I will certainly keep this handy once I run into this use of it. Thank-you guys.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.