Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

grimreaper1377

macrumors regular
Original poster
Oct 20, 2007
218
0
Hello,

I'm fairly new to (obj)c and the main obstacle ive encountered is knowing when to use pointers. Is there any rule? In a lot of sample code from books and screencasts, "int"s and "float"s aren't pointers, while everything else is...Any suggestions?

Thanks!
 

Krevnik

macrumors 601
Sep 8, 2003
4,101
1,312
All Obj-C objects are referenced by pointers. There is no real way around it. The retain/release mechanism is how you deal with an object's scope.

Beyond that rule, the standard C guidelines on when to use pointers apply.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
I think some background in C will help. I guess in my mind a pointer is just a tool. When do you use a hammer? When that's the appropriate tool. A pointer is a memory address, the type of the pointer indicates how many bytes are at the other end and how they should be interpreted. If you need to point to an int that isn't local to a given scope you would use an int pointer. If you need to pass a variable to a function and have its value modified in said function, you'd need to pass a pointer.

It happens that in Objective-C all objects are accessed via pointers as well, but this is the easiest application of pointers to deal with. You never need to reference or derference an object *, you don't have to track the memory in use on the other end of it in the same manner, etc.

-Lee
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.