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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:38:39+00:00 2026-05-16T10:38:39+00:00

I have some confusion about when to use the self keyword during ivars assignment,

  • 0

I have some confusion about when to use the self keyword during ivars assignment, should I use it when I’m trying to assign certain value to ivar or only when I’m trying to access the ivar?

Besides, if I do retainCount on an ivar after its assignment without using self, it prints a count of 1 assuming it was already alloc & init. But if I use self it gives me retain count of 2.

Example Code:

Titles = [[NSMutableDictionary alloc] init];

NSLog(@”Titles before assignment: %d”, [Titles retainCount]); // print 1

Titles = anotherDictionary;
NSLog(@”Titles after assignment: %d”, [Titles retainCount]); // print 1

self.Titles = anotherDictionary;
NSLog(@”Titles after assignment: %d”, [Titles retainCount]); // print 2

Should not I use self during assignment or when should I use? Any help would be really very appreciated as this situation is giving me a lots of doubts that my ivars are not being released the proper way.

Thanks,
Mohsen

Edit: I have another related issue to my above question, should I use self to assign singleton classes properties?

  • 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-16T10:38:40+00:00Added an answer on May 16, 2026 at 10:38 am

    You don’t use self to assign ivars nor to read them. You use it to read/write properties ie call methods/send messges.

    So in your example:

    Titles = anotherDictionary;
    

    Assigned the ivar without calling the setter while

    self.Titles = anotherDictionary;
    

    Was actually

    [self setTitles:anotherDictionary];
    

    Which is usually automatically generated by @syntetize Titles.
    The code of the setter automatically retain objects when the @property has retain flag.

    Recommended practice:
    Once you have the properties that take care of the memory management you should use them. Instead of retian/release objects manually.

    Here is the apple’s guide I was learning from.

    Update:

    Regarding the memory management and just check the apple’s memory management programming guide It answers your question asked in comment. It shows by example how to use the memory management provided by properties.

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

Sidebar

Related Questions

I have some confusion about how to use AtlasLabel. It seems Label consume a
I am trying to learn OOP in PHP, and I have some confusion about
I have some confusion related to the .NET platform build options in Visual Studio
Removed the C tag, seeing as that was causing some confusion (it shouldn't have
I have been starting to write some reasonably large and or confusing MySQL queries
I have some UI in VB 2005 that looks great in XP Style, but
I have some ASP.NET web services which all share a common helper class they
I have some code for starting a thread on the .NET CF 2.0: ThreadStart
We have some input data that sometimes appears with &nbsp characters on the end.
I have some classes layed out like this class A { public virtual void

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.