0

If an object has instance variables that are just pointers to other objects (like NSString), and the variable only holds a reference, not the object itself, why does it need a type? Or why isn't its type something like "NSReference", since that's what it is?

As an example, from "iOS Programming: the Big Nerd Ranch Guide":

In item.h

#import <Foundation/Foundation.h> @interface Item : NSObject { NSString *_itemName; NSString *_serialNumber; int _valueInDollars; NSDate *_dateCreated; } @end 

It seems to me like each variable's type should be something like NSReference. Why are the instance variables declared as strings and date and int if they only hold references to the location of the strings and date and int, not the strings and date and int themselves?

4
  • 1
    See this answer Commented Jan 25, 2015 at 17:24
  • 2
    They have specific types so you know what kind of object is being pointed to (referenced). Commented Jan 25, 2015 at 17:30
  • Because, in objective-c, all objects are pointers. Commented Jan 25, 2015 at 17:48
  • @Scriptable, that is essentially my question, but neither Google nor SO's search returned it, perhaps because I was searching for "objective c", not knowing enough to know if Objective C differed from C on this issue. Commented Jan 26, 2015 at 15:02

2 Answers 2

1

The id type is the one you want. id is an untyped/generic pointer to object.

You can use it if you want, but using it have a major drawback: the compiler will then be unable to verify that you use the pointer correctly. For example:

id foo; foo = [NSString stringWith....]; ... [foo convertFromPolarToCartesian]; 

In the last instruction, the compiler will be unable to see that this will not work because foo as no type and it is unknown if the object it points to will be able to respond to the message (I bet that NSString don't have such a method).

It is really important to detect errors in program as soon as it is possible, and finding errors at compile-time is a right choice, so even if it is possible to use generic pointers don't use them if you know the kind of object they must point to. This will not cover all possible errors, but stupid obvious ones.

Sign up to request clarification or add additional context in comments.

2 Comments

This answers my question, but the first sentence makes me think my question was unclear: I wasn't wanting to find an NSReference pointer, rather, I was wondering why pointers have types. The rest of your answer explained it well :)
You're right. I would have said "id is the type you're looking/asking for"; "want" is too strong...
1

It would be little easier if i explain from the C's point of view. It would be same for the objective-c, as its a wrapper around basic C.

in C

  • character's are 1 byte
  • integer's are 2 byte or 4 byte (depends upon your architecture 32/64 bit)

you can cast both type of pointers into the void* like following:

int *p; char *q; void *a = p; void *b = q; 

when converting it into a void*, compiler could not decide what is the actual data type was. You have to explicitly tell the compiler that the actual data type for a & b was integer & character if you wanna retrieve the actual value pointed by them.

For example:
say you code something like following

int i = 0xABCD;//assume here that integer is 16 bit / 2 byte int *p; p = &i;//now the compiler know that the data type pointed by the p is an integer so read 2 byte as chunk to get the actual value. void *v = p; 

if you really interested to retrieve back the integer you must cast it back to an int* to obtain the value you stored previously like following:

int *q; q = (int*)v;//now *q will give 0xABCD 

If you are really interested to play around something like void* use id in objective-c, which is more similar NSReference you are searching for.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.