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 7842121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:14:49+00:00 2026-06-02T16:14:49+00:00

Is there a difference between using the underscore and using the self keyword in

  • 0

Is there a difference between using the underscore and using the self keyword in Objective-C when calling an @property?

Property declaration:

@property (weak, nonatomic) NSString *myString;

Calling @synthesize on the property:

@synthesize myString = _myString;

Is there a difference if I want to use it in my code? When? In the getter/setter?

self.myString = @"test";
_myString = @"test";
  • 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-02T16:14:50+00:00Added an answer on June 2, 2026 at 4:14 pm

    self.myString = @"test"; is exactly equivalent to writing [self setMyString:@"test"];. Both of these are calling a method.

    You could have written that method yourself. It might look something like this:

    - (void)setMyString:(NSString*)newString
    {
        _myString = newString;
    }
    

    Because you used @synthesize, you don’t have to actually bother writing that method, you can just allow the compiler to write it for you.

    So, from looking at that method, it looks like calling it will do the exact same thing as just assigning a value to the instance variable, right? Well, it’s not so simple.

    Firstly, you could write your own setter method. If you do so, your method would get called, and it could do all sorts of additional things as well as setting the variable. In that case, using self.myString = would call your method, but doing _myString = would not, and thus different functionality would be used.

    Secondly, if you ever use Key Value Observing, the compiler does some very clever tricks. Behind the scenes, it subclasses your class, and overrides your setter method (whether it’s one you wrote yourself or one generated by synthesize), in order to make the calls to willChangeValueForKey: that are needed for Key Value Observing to work. You don’t need to know how this works (although it’s quite interesting if you want some bedtime reading!), but you do need to know that if you want Key Value Observing to work automatically, you have to use setter methods.

    Thirdly, calling the setter method even if you’re relying on synthesize to write one gives you flexibility for the future. You might want to do something extra whenever a value is changed, and at the point you discover you want to do that, you can manually write a setter method — if you’re in the habit of always using self.myString =, then you won’t need to change the rest of your code to start calling the new method!

    Fourthly, the same applies to subclasses. If someone else was to subclass your code, if you use the setters then they could override them to adjust the functionality.

    Any time you access the instance variable directly, you’re explicitly not providing a way for extra functionality to be hooked in at that point. Since you or someone else might want to hook in such functionality in the future, it pays to use the setters all the time, unless there’s a good reason not to.

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

Sidebar

Related Questions

Is there any difference between using keyword unsafe before method or before block of
Is there a difference between using a makefile and a Makefile?
Is there a difference between using exit() or just return statements in main() ?
Is there a difference between using tearDown and setUp versus __init__ and __del__ when
When dealing with a collection of key/value pairs is there any difference between using
In Java, is there a semantic difference between using Illegal (as in IllegalArgumentException )
Is there a measurable performance difference between using INT vs. VARCHAR as a primary
Is there a preference or behavior difference between using: if(obj.getClass().isArray()) {} and if(obj instanceof
Is there a difference between generating multiple numbers using a single random number generator
I'm using them alternately. Is there a difference between them?

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.