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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:19:29+00:00 2026-05-28T18:19:29+00:00

I usually see this question asked the other way, such as Must every ivar

  • 0

I usually see this question asked the other way, such as Must every ivar be a property? (and I like bbum’s answer to this Q).

I use properties almost exclusively in my code. Every so often, however, I work with a contractor who has been developing on iOS for a long time and is a traditional game programmer. He writes code that declares almost no properties whatsoever and leans on ivars. I assume he does this because 1.) he’s used to it since properties didn’t always exist until Objective C 2.0 (Oct ’07) and 2.) for the minimal performance gain of not going through a getter / setter.

While he writes code that doesn’t leak, I’d still prefer him to use properties over ivars. We talked about it and he more or less sees not reason to use properties since we weren’t using KVO and he’s experienced with taking care of the memory issues.

My question is more… Why would you ever want to use an ivar period – experienced or not. Is there really that great of a performance difference that using an ivar would be justified?

Also as a point of clarification, I override setters and getters as needed and use the ivar that correlates with that property inside of the getter / setter. However, outside of a getter / setter or init, I always use the self.myProperty syntax.


Edit 1

I appreciate all of the good responses. One that I’d like to address that seems incorrect is that with an ivar you get encapsulation where with a property you don’t. Just define the property in a class continuation. This will hide the property from outsiders. You can also declare the property readonly in the interface and redefine it as readwrite in the implementation like:

// readonly for outsiders
@property (nonatomic, copy, readonly) NSString * name;

and have in the class continuation:

// readwrite within this file
@property (nonatomic, copy) NSString * name;

To have it completely “private” only declare it in the class continuation.

  • 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-28T18:19:30+00:00Added an answer on May 28, 2026 at 6:19 pm

    Encapsulation

    If the ivar is private, the other parts of the program can’t get at it as easily. With a declared property, the clever people can access and mutate quite easily via the accessors.

    Performance

    Yes, this can make a difference in some cases. Some programs have constraints where they can not use any objc messaging in certain parts of the program (think realtime). In other cases, you may want to access it directly for speed. In other cases, it’s because objc messaging acts as an optimization firewall. Finally, it can reduce your reference count operations and minimize peak memory usage (if done correctly).

    Nontrivial Types

    Example: If you have a C++ type, direct access is just the better approach sometimes. The type may not be copyable, or it may not be trivial to copy.

    Multithreading

    Many of your ivars are codependent. You must ensure your data integrity in multithreaded context. Thus, you may favor direct access to multiple members in critical sections. If you stick with accessors for codependent data, your locks must typically be reentrant and you will often end up making many more acquisitions (significantly more at times).

    Program Correctness

    Since the subclasses can override any method, you may eventually see there is a semantic difference between writing to the interface versus managing your state appropriately. Direct access for program correctness is especially common in partially constructed states — in your initializers and in dealloc, it’s best to use direct access. You may also find this common in the implementations of an accessor, a convenience constructor, copy, mutableCopy, and archiving/serialization implementations.

    It’s also more frequent as one moves from the everything has a public readwrite accessor mindset to one which hides its implementation details/data well. Sometimes you need to correctly step around side effects a subclass’ override may introduce in order to do the right thing.

    Binary Size

    Declaring everything readwrite by default usually results in many accessor methods you never need, when you consider your program’s execution for a moment. So it will add some fat to your program and load times as well.

    Minimizes Complexity

    In some cases, it’s just completely unnecessary to add+type+maintain all that extra scaffolding for a simple variable such as a private bool that is written in one method and read in another.


    That’s not at all to say using properties or accessors is bad – each has important benefits and restrictions. Like many OO languages and approaches to design, you should also favor accessors with appropriate visibility in ObjC. There will be times you need to deviate. For that reason, I think it’s often best to restrict direct accesses to the implementation which declares the ivar (e.g. declare it @private).


    re Edit 1:

    Most of us have memorized how to call a hidden accessor dynamically (as long as we know the name…). Meanwhile, most of us have not memorized how to properly access ivars which aren’t visible (beyond KVC). The class continuation helps, but it does introduce vulnerabilities.

    This workaround’s obvious:

    if ([obj respondsToSelector:(@selector(setName:)])
      [(id)obj setName:@"Al Paca"];
    

    Now try it with an ivar only, and without KVC.

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

Sidebar

Related Questions

I see that this question has been asked before but the context around the
I am sure this question has been asked, and usually I can accomplish this
I usually like to declare my local variables as final but I see repeating
I have asked this before, but the more I look at other code examples
EDIT: I asked around a bit, and apparently my mistake was this: we usually
I asked this question earlier about business logic and presentation logic, and it got
I know this question has been asked a million times in various ways by
I already asked a similar question but this one is a bit different/specific: I'm
First, I know this question has been asked several times before and that in
I imagine this question has been asked and answered, but I cannot find it.

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.