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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:32:25+00:00 2026-06-01T00:32:25+00:00

i have a general question about properties and ivars. ive seen many different examples

  • 0

i have a general question about properties and ivars.

ive seen many different examples to use properties and it confuses me a bit.

method 1 only using a property without a corresponding ivar.

@property (...) Type *name;

@synthesize name;

method 2 using a property and an ivar

@interface{
Type *ivarName;
}
@property (...) Type *name;

@synthesize name = ivarName;

method 3 ignoring properties and working with ivars

@interface{
Type *ivarName;
}
ivar = ...;

i currently use method 1 for most things i do, it just works. but i have startet to wonder if i might be missing something here. i have read a lot of questions about ivars VS properties, but none of them seemed to really care about how they work together.

in most sample projects i’ve seen method 2 is used. so my question is: is there any advantage in defining a property and an ivar, and then assign the property to the ivar, than just having a property?

is the solution as simple as: only with a property can an ivar be set from ‘outside’?

i have read: Must every ivar be a property? and Property vs. ivar in times of ARC but was not able to draw a final conclusion.

  • 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-01T00:32:27+00:00Added an answer on June 1, 2026 at 12:32 am

    is the solution as simple as: only with a property can an ivar be set from ‘outside’?

    Essentially, yes. Ivars in Obj-C are (by default) “protected”, meaning that the compiler won’t allow you to access them externally to the object’s own code. For example, given the following class declaration:

    @interface Dunstable : NSObject
    {
        NSString * crunk;
    }
    @end
    

    You might think you’d be able to access the ivar after creating the object, but trying results in an error:

    Dunstable * d = [[Dunstable alloc] init];
    d->crunk = @"Forsooth";    // Error: "Instance variable 'crunk' is protected
    

    That’s why ObjC uses accessor methods. Defining them manually was mandatory before the advent of declared properties:

    @implementation Dunstable
    
    - (NSString *)crunk {
        return crunk;    // implicit ivar access, i.e. self->crunk
    }
    
    - (void)setCrunk: (NSString *)newCrunk {
        [newCrunk retain];
        [crunk release];
        crunk = newCrunk;
    }
    
    @end
    

    Now, using the @property and @synthesize directives creates those accessor methods for you (as well as the variable itself). (The manual memory management in the setter is of course also obsolete under ARC.)

    It is possible to make an ivar that’s accessible from outside the object:

    @interface Dunstable : NSObject
    {
        @public
        NSNumber * nonce;
    }
    @end
    
    Dunstable * d = [[Dunstable alloc] init];
    d->nonce = [NSNumber numberWithInt:2];    // Works fine
    

    but this isn’t considered good Objective-C style.

    The Objective-C Programming Language doc contains a “Historical Note” about this:

    Note: Historically, the interface required declarations of a class’s instance variables, the data structures that are part of each instance of the class. These were declared in braces after the @interface declaration and before method declarations:
    […]
    Instance variables represent an implementation detail, and should typically not be accessed outside of the class itself. Moreover, you can declare them in the implementation block or synthesize them using declared properties. Typically you should not, therefore, declare instance variables in the public interface and so you should omit the braces.

    This is a pretty big change (I was actually surprised that there’s no syntax given for ivars declared in @interface anymore in that doc), but it’s definitely for the better. You should use declared properties; they do the right thing and make your code cleaner and safer.

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

Sidebar

Related Questions

I have a general question about whether people think you should use a database
I have a general question about the way that database indexing works, particularly in
i have a general question about how sql server evaluates the joins.The query is
I have a general question for the group. I am about to start a
I have a question about the Microsoft PPL library, and parallel programming in general.
I have a general question with regards to immutable collections. I will use Java
I'm a bit of a javascript noob, but I have a general question that
I have a general question about inheritance in the .NET framework, lets say you
I have a general question about DB synchronisation. So, I'm developing a website locally
I have a general XML file containing different properties for my Play! application like:

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.