Amazon.com Widgets Not fully understanding pt.x pt.y
Welcome, Guest. Please login or register.
Did you miss your activation email?
June 20, 2013, 05:36:26 AM
Home Help Search chat Login Register   
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+  Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+  Old Stuff
| |-+  Chapter Study
| | |-+  Chapter 8 - Inheritance
| | | |-+  Not fully understanding pt.x pt.y
Pages: [1]   Go Down
Print
Author Topic: Not fully understanding pt.x pt.y  (Read 1146 times)
SRC
Newbie
*
Posts: 19


Email




« on: August 27, 2011, 08:05:02 PM »

Couple of questions on pgs 173 & 174 (2nd Ed)
In the past we've been setting up class variables like this: XYPoint *myPoint = etc... now I see, origin = [[XYPoint alloc] init]; Is it assumed that origin is a new object variable from XYPoint class? can they be coded either way?
 Huh
[origin setX: pt.x andY: pt.y]; are these the same as variable pt now equals x value etc..? or [pt x] pt is retrieving the value of x? is that the correct way of understanding this?

Thanks
Steve C
Logged
bpedit
Jr. Member
**
Posts: 65


Email




« Reply #1 on: August 28, 2011, 08:06:45 AM »

The reason   origin = [[XYPoint] alloc] init]  works is because you've already declared origin as an instance variable of the class back in the header file. This is something new in the book, an object which needs alloc'ing and init'ing but also an instance variable which needed declaring (in the proper place). So you are merely doing in two lines what you've previously been doing in one. Or:

Rectangle *rectA = [[Rectangle alloc] init];     is the same as

Rectangle *rectA;
rectA =  [[Rectangle alloc] init];

In your second question, origin is the receiver of the message. Origin has two variables, x and y.  setX and setY are the setter methods used to give a particular value to the x and y variables. (When you used @property int x, y; and the @synthesis directive, these two setter methods were automatically created: notice it's just the name of the variable with the word set appended to the front.)  So what do you want to set x with? In this case you are passing another XYPoint object as an argument; this argument will provide the new x and y parameters for the origin. So the  pt.x  means to grab the x variable from the point object for setX to use. ( edit: The point object is the argument passed to the method as pt.)

HTH,
Byrne
« Last Edit: August 28, 2011, 07:30:19 PM by bpedit » Logged
SRC
Newbie
*
Posts: 19


Email




« Reply #2 on: August 28, 2011, 07:22:44 PM »

Yes that does help, I was thinking the @class didn't give enough info for referencing or any detailed use later on.

Thanks
Logged
SRC
Newbie
*
Posts: 19


Email




« Reply #3 on: August 28, 2011, 08:52:50 PM »

One more question about why the program jumps to the setOrigin method as opposed to the origin method.

    [myRect setWidth:5 andHeight:8];
    myRect.origin = myPoint;<---------program from here jumps to setOrigin method
   
    NSLog(@"Origin at (%i, %i)",
          myRect.origin.x, myRect.origin.y);<-----program from here jumps to origin then reads x & y in etc

Are origin & setOrigin connected somehow? they look like separate methods
pt variable seems to have the x,y values of 100,200 the instant the myRect.origin message is sent.

stepping through this I'm not seeing where pt is getting it's vales for x & y

Steve C
Logged
bpedit
Jr. Member
**
Posts: 65


Email




« Reply #4 on: August 29, 2011, 07:45:00 AM »

In the first case you are assigning a value (myPoint) so the compiler is smart enough to translate the .origin on the left of the equals into the setter setOrigin. In the other case there's no assignment, you're merely asking for values so you get the getter origin instead. It's all in the context of where it appears.

BTW, the the message  you refer to in your last sentence is not myRect.origin. The message is setOrigin. The receiver of the message is myRect.

This shortcut way of using setter/getters that have been synthesized is new to version 2.0 of Objective-C. In one other text that I'm using the author really likes this ability because it's similar to the way that members of structures are set in the underlying C language. In another text, the authors won't use the shortcuts. I'm currently not using them so I can better see, and learn the patterns,  when I'm working with structures as opposed to objects.
Logged
SRC
Newbie
*
Posts: 19


Email




« Reply #5 on: August 30, 2011, 09:06:37 PM »

I think I see what is happening, [origin setX: pt.x andY: pt.y]; pt is the argument & also the receiver invoking the method '-(int) x' which returns x, that was set in the method 'setX:andY:' earlier, 'self' (origin) gets these values.

-(int) x
{
    return x;
}
-(int) y
{
    return y;
}

-(void) setX:(int) xVal andY: (int) yVal
{
    x = xVal;
    y = yVal;
}

This is what I determined in debug mode, Is this the right way to describe it?

Steve C
Logged
SRC
Newbie
*
Posts: 19


Email




« Reply #6 on: August 30, 2011, 09:31:54 PM »

Now your saying myRect.origin = myPoint; invokes the setOrigin method, is this because the origin method has return origin, the compiler then goes to where the origin was defined? Which is the setOrigin: pt method ? it wouldn't  work with myRect.setOrigin = myPoint;

Steve C
Logged
bpedit
Jr. Member
**
Posts: 65


Email




« Reply #7 on: August 31, 2011, 08:38:47 AM »

myRect.setOrigin = myPoint   is nonsense. When you use the dot operator (a simple period) you never write "set". So when you write, for example, origin.x, it can mean either get or set. Which one it means depends on the context of how it's used. If it's assigned to something else like pos = origin.x, it's a getter. If something else is assigned to it like origin.x = pos, it's a setter.

pos = origin.x              pos = [origin x]              both the same

origin.x = pos              [origin setX: pos]            both the same
Logged
SRC
Newbie
*
Posts: 19


Email




« Reply #8 on: September 02, 2011, 09:40:21 PM »

What are two dots used for? I had always thought the dot was just replacing the space and no need for the brackets. But when I look at code :

NS_INLINE NSRect NSMakeRect(CGFloat x, CGFloat y, CGFloat w, CGFloat h) {
    NSRect r;
    r.origin.x = x;
    r.origin.y = y;
    r.size.width = w;
    r.size.height = h;
    return r;
}

I have thought it was always this way [receiver message] or receiver.message are two dots in the above example r.size.width - r is the receiver then size and width are arguments? r.origin.x origin with argument x? It seems the're a variety of situations. I'm confused with the two dots I can't explain it.
Logged
seerex
Full Member
***
Posts: 177


Email




« Reply #9 on: September 05, 2011, 09:13:06 AM »

When you use 2 dots, you are accessing an instance member. for instance, in this interface declaration:

Code: (Objective-C)
@interface MyPoint : NSObject
{
XYPoint *newPoint;
}

// other code

You actually create a new object, within a class file. So, (assuming you allocated / initialized newPoint properly) you will need some way to set it's instance variables. Lets assume this is the XYPoint file:
Code: (Objective-C)
@interface XYPoint : NSObject
{
int x;
int y;
}
@property int x, y;
// other code

You have synthesized your x and y instance members, so you can access them using the dot operator. Now, lets say you create a new myPoint, and you want to set the x and y of the instance member object (myPoint) declared in your myPoint interface file. You could say:
Code: (Objective-C)
[[myPoint newPoint] setX = 10];

or you can say:
Code: (Objective-C)
myPoint.newPoint.x = 10;

I hope it makes sense. In short, i guess you can say that 1 dot means you access a instance member of the class itself who calls / invokes / is receiver of message. If you have 2 dots, you are accessing another instance member, in another object from another class stored in the message invoker. Atleast that is true for this case.

Hope it makes sense. Sorry if i made a typo writing this, i just quickly wrote it up. I think it's right though, if not, someone else it bound to smite me with a heavy bolt of lightning Smiley
Logged

Best Regards -
J.B.J
If you think i helped you could return the favor Wink

Subscribe to my channel on youtube: http://www.youtube.com/user/JBJProgramming

My apps:
- iTap Fruits (all countries)
- Toilet-Quiz (Denmark only)
- Zoo Kids (Denmark only)
- Diablo Item Database (All countries)
skochan
Administrator
Hero Member
*****
Posts: 3109







« Reply #10 on: September 05, 2011, 04:32:38 PM »

In addition to what seerex write, there are some other points worth noting about the dot operator.

One of the problems that was introduced with the introduction of the dot operator into Objective-C 2.0 was the fact that operator was already used as the structure member operator from the underlying C language.   Therefore, when determining whether the dot operator is begin used a structure member operator or to access a property, you have to see whether it's being applied to a structure or an object.   In the example you show, NSRect is a structure (covered in Chapter 13), whose members are origin and size.  Those are also structures.  So the expression r.origin.x accesses the x member of the origin structure, which is a member of the NSRect structure  r.   

You can have both operators used in the same expression, which can lead to some confusion for new programmers.  In the text, we have a Rectangle object which has an origin instance variable, which is an XYPoint object.  So in that case, writing myRect.origin.x  actually does use the getter methods to get the value of x, based on the fact that you're dealing with objects and thus the use of the dot operator is clear.

Hope this helps rather than confuses more!

Cheers,

Steve
Logged
SRC
Newbie
*
Posts: 19


Email




« Reply #11 on: September 05, 2011, 09:19:09 PM »

Hope I don't wear you guys down with this,

from Seerex "someone else it bound to smite me with a heavy bolt of lightning"

It won't be me, I appreciate the time everyone has spent trying to help. I'm just sorry I'm a brick wall in understanding this stuff. I have learned allot this weekend on the dot syntax when it is only setter/getter with properties. I've rewritten the examples with both dot & bracket types successfully. But I'm still not seeing whats going on here:

After this is executed: [myRect setOrigin:myPoint]; I changed from myRect.origin = myPoint;

In the debug screen:

pt =   XYPoint *   0x100114910
NSObject   NSObject   {...}
x =   int   100
y =   int   200

I keep thinking that pt is a variable that can hold only one value at a time, but I see it has Both the x & y instance variables. trying to find the right wording here.. is pt considered like a Class, I know it is of "Class XYPoint". Wouldn't that be the only way it can hold many variables and objects? I never thought pt would hold both x & y.

In other words I prefer the Bracket syntax but here again I can't find the correct way to state what details are going on with this: [[myRect origin] x] same as myRect.origin.x

If always read from left to right "The result of applying the origin method to myRect (or storing the result in the myRect variable), then to that get the value of x" Am I way off here? pt.x would be like indexing or selecting from the many possible variables held within that object?

Steve C
Logged
SRC
Newbie
*
Posts: 19


Email




« Reply #12 on: September 05, 2011, 09:34:06 PM »

Found this on the network, they are supposed to be equivalent :

// each member of the path is an object
x = person.address.street.name;
x = [[[person address] street] name];

If the Dot syntax or bracket syntax goes beyond the single setter/getter I'm confused.

To me the above looks like address, street & name are all methods being applied in succession to the person instance. but I have a feeling thats wrong. ??

Logged
skochan
Administrator
Hero Member
*****
Posts: 3109







« Reply #13 on: September 06, 2011, 12:34:26 AM »

Yes, it's calling those methods in succession and so the two statements are equivalent.   From reading that statement, I would assume that person is an object that has a property called address, that address that is returned has a property called street, and that the street that is returned has a property called name.  Typically, these properties will map one-to-one to instance variables, but they don't have to (they can call methods that do other work beside just fetch the value of the corresponding instance variable).

Cheers,

Steve
Logged
seerex
Full Member
***
Posts: 177


Email




« Reply #14 on: September 06, 2011, 08:09:49 AM »


If always read from left to right "The result of applying the origin method to myRect (or storing the result in the myRect variable), then to that get the value of x" Am I way off here? pt.x would be like indexing or selecting from the many possible variables held within that object?

Well, pt is a variable, but a variable you say to hold a reference, or pointer to an XYPoint object. (if you are unclear of this whole variable's referencing objects, maybe my video could help http://www.youtube.com/watch?v=HXQ7nco_M8w )

So, yea pt is an object, thus meaning it can hold any value equivalent to the number of declared instance members. In this case, x and y. So when using that dot-syntax or the [ ] message call, you are first accessing the first class, then you are accessing a property / instance member / object contained in that class, and then you access that class's instance variables.

It can be hard to grasp, but keep at it Smiley you'll know when it clicks Smiley ty to do some experiments as well.

follow me on www.jbjprogramming.com
« Last Edit: September 06, 2011, 08:22:15 AM by seerex » Logged

Best Regards -
J.B.J
If you think i helped you could return the favor Wink

Subscribe to my channel on youtube: http://www.youtube.com/user/JBJProgramming

My apps:
- iTap Fruits (all countries)
- Toilet-Quiz (Denmark only)
- Zoo Kids (Denmark only)
- Diablo Item Database (All countries)
Pages: [1]   Go Up
Print
Jump to:  



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.