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

jwei92

macrumors newbie
Original poster
Jun 5, 2011
7
0
I've looked through a couple threads and am still lost on returning pointers.

I am currently in the process of learning Obj-C with not previous programming background.

I know some basics, but the only thing I cannot seem to grasp is in Kochan's Objective-C 2.0's book (Chapter 8 - A Point Class and Memory Allocation)

I know how to allocate, but here are the parts that confuse me:

Code:
@interface Rectangle:NSObject 
{
    int width, height;
    [I]XYPoint *origin;[/I]
}

[I]-(XYPoint*)origin;
-(void)setOrigin:(XYPoint*)pt;[/I]

I do not understand why the method returns pointers and such. Sorry if my question isn't clear, but can someone elaborate on why you return a pointer and not just an integer?
 
All objects are gotten to in Objective-C via a pointer. That's it. Passing an object? You pass a pointer. Getting one back? Pointer. End of story. If it's just an int, sure... just return the int. There may be some reason to return a pointer to a primitive (maybe you want to modify it and have that affect something else that also uses the value in that memory location), but with objects there is no question.

-Lee
 
That was the point I got to in that same book. When he started in with pointers and self it got complicated. I then put that book down and Thought, Objective C is built on top of C. So I got the book "Learn C on the Mac" http://www.amazon.com/Learn-C-Mac-Dave-Mark/dp/1430218096 and that book was pretty easy to follow, this was end of last year.

I have now started back on learning Objective C and from the C book I understand all of the pointer concepts, data structures , functions and so on. Since I liked the Aprees book Learn C on the Mac I decided to start with Learn Objective C for Absolute Beginners by the same company. I just started reading it the other day.

If it gets hard for you, like me, then I recommend taking it a step back to something simpler. This is from experience.

-Lars
 
As an Amazon Associate, MacRumors earns a commission from qualifying purchases made through links in this post.
That was the point I got to in that same book. When he started in with pointers and self it got complicated. I then put that book down and Thought, Objective C is built on top of C. So I got the book "Learn C on the Mac" http://www.amazon.com/Learn-C-Mac-Dave-Mark/dp/1430218096 and that book was pretty easy to follow, this was end of last year.

I have now started back on learning Objective C and from the C book I understand all of the pointer concepts, data structures , functions and so on. Since I liked the Aprees book Learn C on the Mac I decided to start with Learn Objective C for Absolute Beginners by the same company. I just started reading it the other day.

If it gets hard for you, like me, then I recommend taking it a step back to something simpler. This is from experience.

-Lars

+1 These are the books I started with. They are much easier to digest than Kochan's book. Kochan's book is a great 3rd book to read.
 
As an Amazon Associate, MacRumors earns a commission from qualifying purchases made through links in this post.
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.