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

  • SEARCH
  • Home
  • 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 6828961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:26:40+00:00 2026-05-26T22:26:40+00:00

Possible Duplicate: How does an underscore in front of a variable in a cocoa

  • 0

Possible Duplicate:
How does an underscore in front of a variable in a cocoa objective-c class work?

It is not fully clear to me (other than for readability of the code), why you wanna create an internal variable with an underscore prefix when you create the property.

Since everything is handled internally, why bother to do so, since we do not add any code to the getter and setter?

And even if i gotta add some code to the getter or setter, i do not see why i cannot just do the check on myvar instead than having to check _myvar and then assign it to myvar.

Can anyone give me some explanation, other than “do it because that’s what everyone does ?” I would like to understand the whole reason behind this practice (that seems to be pretty common even if there is no custom code for the getter and setter).

Thanks!

  • 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-05-26T22:26:41+00:00Added an answer on May 26, 2026 at 10:26 pm

    An Objective-C property usually has a backing instance variable (I guess you know the difference between a property and an instance variable).

    The property may have a different name than the instance variable.

    For instance, you may have an instance variable named x, with a property named y.

    You can synthesize the y property to the x variable using:

    @synthesize y = x;
    

    Now about the underscore.

    It’s a common practice to use an underscore prefix for instance variables, to prevent naming collisions, or compiler warnings (shadowed variable), when having for instance a method argument with the same name as an instance variable.

    The underscore prefix also makes clear that you are referring to an instance variable.

    By using the underscore prefix for instance variables, you’re free to use the name without the underscore in method’s arguments, stack variables, etc.

    But when using a property, you usually don’t want the user to write an underscore.

    So you usually have an x property for an _x instance variable.

    This is why you write:

    @synthesize x = _x;
    

    Let’s take an example:

    @interface Test: NSObject
    {
        int x;
    }
    
    @property( readonly ) int x;
    
    @end
    

    This is quite common… But now imagine this in the implementation:

    - ( id )initWithX: ( int )x
    {}
    

    We have are a naming collision.

    Inside our method, x will refer to the method’s argument. And there is no pretty way to access the x instance variable.

    Depending on your compiler’s warning flags, this may also generate a warning (-Wshadow).

    If you use an underscore prefix for your instance variable, everything is just simple:

    - ( id )initWithX: ( int )x
    {
        if( ( self = [ super init ] ) )
        {
            _x = x;
        }
    
        return self;
    }
    

    No conflict, no naming collision, improved reading… Just a nice way…

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

Sidebar

Related Questions

Possible Duplicate: How does an underscore in front of a variable in a cocoa
Possible Duplicate: How does an underscore in front of a variable in a cocoa
Possible Duplicate: How does an underscore in front of a variable in a cocoa
Possible Duplicate: How does an underscore in front of a variable in a cocoa
Possible Duplicate: How does an underscore in front of a variable in a cocoa
Possible Duplicates: How does an underscore in front of a variable in a cocoa
Possible Duplicate: Synthesized property and variable with underscore prefix: what does this mean? I've
Possible Duplicate: Synthesized property and variable with underscore prefix: what does this mean? The
Possible Duplicate: What does a type followed by _t (underscore-t) represent? While typing in
Possible Duplicate: How does the Google Did you mean? Algorithm work? Suppose you have

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.