Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8009755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T18:32:12+00:00 2026-06-04T18:32:12+00:00

I was trying something out as I was learning about @property and @synthesize .

  • 0

I was trying something out as I was learning about @property and @synthesize. I thought @property created the instance variables and @synthesize created the getter and setter methods for it.

#import <Foundation/Foundation.h>

@interface XYPoint : NSObject 
@property int x, y;

-(void) setX: (int) xVar andY: (int) yVar;

@end

I didn’t synthesize x and y in my implementation file, but I was wondering why I was allowed to do both:

-(void) setX:(int)x {
    x = x;
}

-(void) setY:(int)y {
    y = y;
}

but got an error message about undeclared identifiers x and y when I did this:

-(void) setX:(int)xVar andY:(int)yVar {
    x = xVar;
    y = yVar;
}

Also, why didn’t -(void) setY:(int)y and -(void) setX:(int)x have to be declared in the interface file?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T18:32:13+00:00Added an answer on June 4, 2026 at 6:32 pm

    I thought @property created the instance variables and @synthesize created the getters and setters method for it.

    This is not quite correct. The @property declaration basically just makes a promise to other classes: “You can ask me for an int named x, and I will also respond to attempts to give me an int to store under that name.” The declaration itself doesn’t create anything in the class, either methods or an ivar.

    It’s @synthesize that does both those things. You can implement your own setter, getter, and ivar yourself, or you can synthesize them (or any combination).

    So the reason that you’re getting that error when you don’t use @synthesize is that no ivars have been created, so x and y aren’t names you can use inside the class.

    In the first case, you’re not doing what you seem to think you’re doing. In this method:

    -(void) setX:(int)x {
        x = x;
    }
    

    x is the name of the parameter, and you’re just assigning it to itself. (You can’t have two variables with the same name in the same scope. If you had created an ivar named x, you’d get a compiler warning about the method parameter shadowing the ivar. That means that the parameter name, being the same as the ivar, won’t allow you to access the ivar.)

    If you change that to:

    -(void) setX:(int)newX {
        x = newX;
    }
    

    you’ll see the same error about x being undeclared.

    Also, why didn’t -(void) setY:(int)y and -(void) setX:(int)x have to be declared in the interface file?

    They are declared by the @property directive.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying out something in jQuery and stumbled upon a site http://pupunzi.com/ , on
I am trying to figure out something - Every time I use AVAudioPlayer I
Trying to figure out the best way to do this: Should I do something
I'm trying to figure out how to show something like Showing 1-10 of 52
I'm trying to figure out how to do something similar to the twitter silverlight
I'm working on a little something and I am trying to figure out whether
Something I don't enjoy about programming is learning a new API. For example, right
I'm just starting out learning C# and I've become stuck at something very basic.
For learning purposes I am trying out running a simulation as a sequence with
I am learning Python and trying to figure out an efficient way to tokenize

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.