-1

Possible Duplicate:
objective c difference between id and void *
why most of the objects we create in iphone are pointers

According to Stanford university course, 2010/2011

Lecture 3

The guy made something strange there (at least for me), which is that

NSString *digit = sender.titlelabel.text;

Why is digit a pointer?

Community
  • 1
  • 1
TheNavigat
  • 864
  • 10
  • 30
  • 1
    Among others: http://stackoverflow.com/search?q=%5Bobjc%5D+why+use+pointers&submit=search – jscs Mar 09 '12 at 20:52
  • 1
    Actually, this is the answer I meant to point to: http://stackoverflow.com/questions/2189212/why-object-dosomething-and-not-object-dosomething/2214980#2214980 It attempts to describe exactly what the difference is between, say, `NSString*` and `NSString` (without the *). – bbum Mar 09 '12 at 21:20

5 Answers5

2

The type of your digit is id, which is just basically just a C pointer to a certain struct. All references to objects in Objective-C have this primitive type, regardless of the Class of the object. So the answer to your question is, unfortunately, because that's the way Objective-C works.

So whether you're declaring an NSString*, or an UITableViewController*, or MyClass*, your variable has type id. This is the primary means by which the language implements polymorphism. So, for example, the following declarations are equivalent:

NSString *digit;
id digit;

And it's true of method prototypes as well. These are equivalent:

-(UITableViewCell *)tableView:(UITableView)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
-(id)tableView:(id)tableView cellForRowAtIndexPath:(id)indexPath;

A variable of type id is not an object itself, it is a pointer to an object. It is the handle with which you manipulate an object. Objective-C does all of the class compatibility work at runtime.

Hope this helps. Any questions?

Updates

That's right: int, float, double, char, void, and the pointer combinations, are all C primitive types. You can and will still use these quite a bit, and they are just what they are in a C program. But Objective-C adds the id type as a way to bridge the gap between the primitive typing of C and the very high-level typing of objects by the Objective-C system. id itself is typedef'd as a pointer to a simple struct in objc.h. At the level of the compiler and the language itself, there really isn't too much meaning to the type. For example, you'll almost never declare an array of ids, certainly never perform any arithmetic with them.

In fact, it's not too far a stretch to say that Objective-C is just plain vanilla C with some added syntax (particularly, the square-bracket notation for method invocation), a few extra primitive types (id, IMP, SEL), and a big runtime library. It's this runtime library that handles all things Object-Oriented.

Anyway, to answer your question, when you're actually programming, you will most often (99% of the time) just use class names to declare your variables - NSString *, NSData *, UITableViewController *, and so on. And the compiler will know what you're talking about, and issue a warning if you write code that clearly tries to put an NSString* where an NSData* is expected. But the actual meaning of those types really exists only at runtime.

I've digressed a little, but I'm not sure where your understanding is failing you, so I thought I'd just explain things a bit. You might want to read Apple's The Objective-C Programming Language to get a feel for the language.

QED
  • 9,803
  • 7
  • 50
  • 87
  • But some stuff are declared like this int X float Y I know these are C stuff (I'm actually C/C++ intermediate~professional algorithm programmer), so ONLY Objective-C objects use this way or what? – TheNavigat Mar 09 '12 at 21:16
  • Checking this: http://stackoverflow.com/questions/2189212/why-object-dosomething-and-not-object-dosomething/2214980#2214980 I figured out the same point. But since it uses pointers, all objects would be accessible by all other objects. It would just work with the memory. It turns a blind eye to whatever it belongs to, and nothing can prevent it. Right? – TheNavigat Mar 10 '12 at 10:07
  • That's right, kind of. It's the *compiler* that turns a blind eye to OO. Objective-C has two levels of type - primitive and OO. Primitive types - int, float, et al from C - along with Obj-C's additions of id, SEL, IMP - are primitive and enforced by the compiler. The OO typing system - *all of it* - NSString, etc, with NSObject at root - are OO types, but OO semantics (encapsulation, polymorphism, etc) are enforced by the runtime system. The compiler *will* pick up on obvious OO type conflicts, but these will only result in a warning, not an error. Read the docs, I'm out of characters! – QED Mar 10 '12 at 15:57
1

NSString is an Objective-C class and all object references in Objective-C are pointers. I would suggest reading through some of the documentation such as Learning Objective-C A Primer:

Notice the * in the first declaration. In Objective-C, object references are pointers. If this doesn’t make complete sense to you, don’t worry—you don’t have to be an expert with pointers to be able to start programming with Objective-C. You just have to remember to put the * in front of the variable names for strongly-typed object declarations. The id type implies a pointer.

Joe
  • 56,979
  • 9
  • 128
  • 135
  • Reading reading reading... I like reading, but most of stuff are just pages and pages, and you can't find the single line containing your answer for ages Anyway, that solves me. I concluded that NSString's are ALWAYS pointers. By the way, what other objects are always pointers? EDIT: After seeing Josh's answer, I found it. Thanks :) – TheNavigat Mar 09 '12 at 20:55
  • I added a quote from the link I posted. The only thing required to be a pointer in Objective-C are references to the classes (an instance of the class). So primitive types like ints, doubles, or even structs can be declared without a pointer. Also if you were to mix C++ and Objective-C the C++ classes can still be declared on the stack (no pointer). – Joe Mar 09 '12 at 20:58
  • No, NSString's are not always pointers. One *refers* to NSStrings usually by pointers. – Matthias Mar 09 '12 at 20:59
  • Ah... Doesn't he want to "create" an NSString called digit to put text there? Why does he make it as a pointer? – TheNavigat Mar 09 '12 at 21:02
  • @joe yeah, there was a race condition, and I forgot to address my comment. – Matthias Mar 09 '12 at 21:11
1

It's not a digit, it's the "text" from the label, which is (I'm guessing) a string of integers and such to express the time.

So, all NSString types are declared as pointers in Obj-c.

 sender.titlelabel.text;

Returns a NSString *

Remember, it's the same as:

NSString *str  = [sender.titlelabel getText]; 
mr-sk
  • 13,174
  • 11
  • 66
  • 101
1

Because text is too. Or more preceisly, because the getText message returns a pointer.

Matthias
  • 8,018
  • 2
  • 27
  • 53
  • Are you sure about that point? I guess that's the most clear and convincing answer for it, that getText returns a pointer :/ – TheNavigat Mar 09 '12 at 21:02
  • While that is true about matching the return type, it is more in-depth than that. When you declare a reference to an Objective-C class it will always be declared as a pointer. – Joe Mar 09 '12 at 21:05
  • @Joe While you are right, there is even more in-depth than that. One *could* construct an ObjC object (as a stack object) and reference it like, e.g., a struct. However, this is not how it is usually done. Rather, one lets it's class construct that (anonymous) object; and the class returns a pointer. – Matthias Mar 09 '12 at 21:19
0

You can find an intersting about why it has to be a pointer:

NSString and Pointers

I Hope it will help you to understand it in a Objective-C way.

ChapMic
  • 26,954
  • 1
  • 21
  • 20