noob quastion about Points

Discussion in 'iOS Programming' started by woker2012, Jul 2, 2012.

  1. woker2012 macrumors newbie

    Joined:
    Jul 2, 2012
    #1
    hey,
    lets say i want to make a method that take a point class object (x,y) as an argument.. i cant figure what`s the deference between:

    1) -(void) getPoint : (Point*) aPoint;

    2) -(void) getPoint : (Point) *aPoint;

    where should i put the * sign? and why?

    tnx alot!
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    Have you tried them both? What happened in each case?
     
  3. taedouni, Jul 2, 2012
    Last edited: Jul 2, 2012

    taedouni macrumors 65816

    Joined:
    Jun 7, 2011
    Location:
    California
    #3
    I am assuming that you are using the book Programming in Objective C -4e by Kochan. Here is my code as follows:


    From my understanding the * is used as a pointer/reference. Basically it references where the data is stored (if I am not mistaken, I am still new to all of this, I did a little C++ in College and other than that I used MATLAB).
    You only want to use the * when you define an object from your class, see my code: XYPoint *newpoint=[XYPoint new];

    I am pretty positive that (point) is incorrect since it is not a type (e.g. int, double, float, char).


    If I am wrong please forgive me. Good luck and let me know how it worked out.
     
  4. taedouni macrumors 65816

    Joined:
    Jun 7, 2011
    Location:
    California
    #4
    Code:
    #import <Foundation/Foundation.h>
    
    
    //Interface   (XY cordinate class)
    
    @interface XYPoint : NSObject
    
    -(void) print;
    -(void) SetPointX : (int) x;
    -(void) SetPointY : (int) y;
    -(int) Xpoint;
    -(int) Ypoint;
    
    
    @end
    
    //Implementation (XYPoint)
    @implementation XYPoint
    
    {
        int Xpoint;
        int Ypoint;
    }
    
    -(void) print
    {
        NSLog(@"The XY point is (%i,%i)",Xpoint,Ypoint);
    }
    
    -(void) SetPointX: (int) x
    {
        Xpoint=x;
    }
    
    -(void) SetPointY: (int) y
    {
        Ypoint=y;
    }
    
    -(int) Xpoint
    {
        return Xpoint;
    }
    
    -(int) Ypoint
    {
        return Ypoint;
    }
    @end
    
    int main(int argc, const char * argv[])
    {
    
        @autoreleasepool {
            
            XYPoint *newpoint=[XYPoint new];
            [newpoint SetPointX:2];
            [newpoint SetPointY:1];
            
            [newpoint print];
            
            
            
        }
        return 0;
    }
    
    
     
  5. woker2012 thread starter macrumors newbie

    Joined:
    Jul 2, 2012
    #5
    well, first of all tnx alot for helping me!
    second, as i see it now when i want to make an object from a class its gonna look like this:

    Someclass *anObject = [[Someclass alloc] init];

    i`m defining a pointer to an object - anObject actually contain an address of the memory that contain the object , that`s seems quite reasonable..

    on the other hand here is my guess about whats happening in this declaration:

    -(Point*) getPoint : (Point*) aPoint;

    here i defined a method that gets a reference (an address) of the object that was inserted as the mathod was activated, so if i want to actually get the values aPoint contain i`ll direct straight to the instance variable:

    aPoint.x;
    aPoint.y;

    cuz if i`ll do that:
    aPoint = x;

    i`ll get the adress of aPoint object (thats a 50\50 guess for me)

    the other case:

    -(void) getPoint : (Point) *aPoint;

    i just cant figure the deferences!

    i need to know exectly what does the sign * refer to, i also know that when u fool around with pointers u also use the & sign!

    i think me and other ppl would realy like someone to clarify this issue.

    tnq bros!
     
  6. woker2012 thread starter macrumors newbie

    Joined:
    Jul 2, 2012
    #6
    oh yes this gave me an error
    -(void) getPoint : (Point) *aPoint;

    and the other one worked just fine
    but i hate saying its working or not working or i`m doing it this way because "thats the way it is" i wanna know why!?
     
  7. chown33, Jul 3, 2012
    Last edited: Jul 3, 2012

    chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #7
    The one that worked is working because it's correct. That is, it correctly follows the language syntax.

    The one that doesn't work is not working working because it's incorrect. It doesn't follow the correct language syntax.

    If your question is, "Why is the language syntax that way?", the answer is, "Because that's the way the language creator designed it." If you don't see why that might be so, then think about what kind of sense it makes to break a type declaration apart, so some parts are outside the parentheses and some parts are inside.

    Parentheses are there for a reason: grouping. If you break a logical grouping apart, it loses its meaning. Like alphabetizing the words in a sentence. It may make sense if you're doing something that needs alphabetical order, but it destroys the meaning that depends on position.


    If you're having trouble understanding the book or tutorial you're learning from, you'll have to tell us exactly which book or tutorial that is, and exactly where you're having trouble (what page).
     
  8. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    Why are you guessing about this? This is pretty basic stuff in Objective-C that is described and detailed in numerous resources.

    Actually, you are not accessing them directly via the instance variables, in this case, but rather through the properties set up for x and y.

    Again, why guess? And, no, you don't get the address of aPoint object. You are assigning aPoint the value of x.
     
  9. woker2012 thread starter macrumors newbie

    Joined:
    Jul 2, 2012
    #9
    by saying guess i ment - that`s how i understand it, one cannot acutally write code by guessing...

    can u please explain:

    Point *aPoint = [[Point alloc] init];
    aPoint = x;

    what is the value of x?

    &aPoint = x;

    what does that means?
     
  10. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #10
    I think you should probably tell us what resources you are using to learn Objective-C first. Be as specific as you can, please.
     
  11. woker2012 thread starter macrumors newbie

    Joined:
    Jul 2, 2012
    #11

    if its a syntax issue then everything is clear to me...are you sure that`s the reason? so in mathod declaring i`ll use:

    (Point*) aPoint

    and declaring an object will be:

    Point *aPoint

    i`m learning from stephen kochan 4th edition (awesome book btw)

    i just wondered if its a syntax issue or not?
    if not i wanna know why there are deferences?

    again if its syntax its all clear to me

    tnx !
     
  12. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #12
    Declaring a method parameter always has parentheses around the declared type. Always.

    Declaring a variable never has parentheses around the declared type. (It is possible to have type declarations that are complex enough to need parentheses, or to have type declarations that involve functions (which are then identified as a name followed by parens), but never parentheses around the complete declared type.)

    And what would it be other than "a syntax issue"? That's all there is at that point: syntax. There's no semantics or runtime behavior there; only syntax.
     
  13. woker2012 thread starter macrumors newbie

    Joined:
    Jul 2, 2012
    #13
    syntax is syntax, thank you
     

Share This Page