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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T03:57:31+00:00 2026-05-18T03:57:31+00:00

What is the semantic difference between these 3 ways of using ivars and properties

  • 0

What is the semantic difference between these 3 ways of using ivars and properties in Objective-C?

1.

@class MyOtherObject; 
@interface MyObject {
}
@property (nonatomic, retain) MyOtherObject *otherObj;

2.

#import "MyOtherObject.h"
@interface MyObject {
    MyOtherObject *otherObj;
}
@property (nonatomic, retain) MyOtherObject *otherObj;

3.

#import "MyOtherObject.h"
@interface MyObject {
    MyOtherObject *otherObj;
}
  • 1 1 Answer
  • 2 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-18T03:57:32+00:00Added an answer on May 18, 2026 at 3:57 am

    Number 1 differs from the other two by forward declaring the MyOtherObject class to minimize the amount of code seen by the compiler and linker and also potentially avoid circular references. If you do it this way remember to put the #import into the .m file.

    By declaring an @property, (and matching @synthesize in the .m) file, you auto-generate accessor methods with the memory semantics handled how you specify. The rule of thumb for most objects is Retain, but NSStrings, for instance should use Copy. Whereas Singletons and Delegates should usually use Assign. Hand-writing accessors is tedious and error-prone so this saves a lot of typing and dumb bugs.

    Also, declaring a synthesized property lets you call an accessor method using dot notation like this:

    self.otherObj = someOtherNewObject; // set it  
    MyOtherObject *thingee = self.otherObj; // get it 
    

    Instead of the normal, message-passing way:

    [self setOtherObject:someOtherNewObject]; // set it
    MyOtherObject *thingee = [self otherObj]; // get it 
    

    Behind the scenes you’re really calling a method that looks like this:

    - (void) setOtherObj:(MyOtherObject *)anOtherObject {
    
        if (otherObject == anOtherObject) {
            return;  
        }
    
        MyOtherObject *oldOtherObject = otherObject; // keep a reference to the old value for a second
        otherObject = [anOtherObject retain]; // put the new value in  
        [oldOtherObject release]; // let go of the old object
    } // set it
    

    …or this

    - (MyOtherObject *) otherObject {  
        return otherObject;
    } // get it
    

    Total pain in the butt, right. Now do that for every ivar in the class. If you don’t do it exactly right, you get a memory leak. Best to just let the compiler do the work.

    I see that Number 1 doesn’t have an ivar. Assuming that’s not a typo, it’s fine because the @property / @synthesize directives will declare an ivar for you as well, behind the scenes. I believe this is new for Mac OS X – Snow Leopard and iOS4.

    Number 3 does not have those accessors generated so you have to write them yourself. If you want your accessor methods to have side effects, you do your standard memory management dance, as shown above, then do whatever side work you need to, inside the accessor method. If you synthesize a property as well as write your own, then your version has priority.

    Did I cover everything?

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

Sidebar

Related Questions

In Java, is there a semantic difference between using Illegal (as in IllegalArgumentException )
Could someone please clarify for me the semantic difference between these two: self.foo =
Is there a semantic difference between these two declaration or is it only syntactic
In Haskell, lifted type products mean that there's a semantic difference between (a,b,c) and
What is the difference between Semantic-markup and Semantic Web ? Are both same concepts?
Is there any semantic difference between writing assertThat(object1, is(equalTo(object2))); and writing assertThat(object1, equalTo(object2))); ?
Is there any semantic difference between ToXXXX conversion methods and AsXXXX conversion methods in
Hi I have a strange situation here: # Is there a semantic difference between
What is the difference between these two innerclass declarations? Also comment on advantages/disadvantages? case
What is the difference between using the intersection() method or the & operator on

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.