Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jun 5, 2011, 03:20 PM   #1
jwei92
macrumors newbie
 
Join Date: Jun 2011
Returning a pointer

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;
    XYPoint *origin;
}

-(XYPoint*)origin;
-(void)setOrigin:(XYPoint*)pt;
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?
jwei92 is offline   0 Reply With Quote
Old Jun 5, 2011, 03:29 PM   #2
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: Dallas, TX
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
lee1210 is offline   0 Reply With Quote
Old Jun 5, 2011, 03:38 PM   #3
larswik
macrumors 65816
 
Join Date: Sep 2006
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-Da.../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
larswik is offline   1 Reply With Quote
Old Jun 6, 2011, 10:23 AM   #4
Shawnpk
macrumors 6502
 
Join Date: Jan 2011
Location: Los Angeles, CA
Quote:
Originally Posted by larswik View 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-Da.../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.
__________________
My iOS Apps: Schedules, Pitch Fever, Gridiron Fan
Shawnpk is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Disappearing mouse pointer J.C OS X 10.8 Mountain Lion 54 Apr 22, 2014 07:48 AM
Point out a Pointer Mistake ArtOfWarfare Mac Programming 10 Oct 13, 2013 11:48 AM
Unsmooth mouse pointer? nariza7 MacBook Air 0 Sep 8, 2013 10:52 PM
Pointer Question larswik iPhone/iPad Programming 8 Feb 27, 2013 03:10 PM
Change pointer color MacPC OS X 10.8 Mountain Lion 0 Aug 22, 2012 02:08 PM

Forum Jump

All times are GMT -5. The time now is 03:35 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC