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

  • Home
  • SEARCH
  • 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 7871529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:56:00+00:00 2026-06-03T01:56:00+00:00

Possible Duplicate: Setters and Getters (Noobie) – iPhone SDK I am a beginner here.

  • 0

Possible Duplicate:
Setters and Getters (Noobie) – iPhone SDK

I am a beginner here. I have just started learning iOS for the last two months and I do not have any programming background. (Little bit of Java though). Can anyone please explain what is getter and setter in Objective C? What is the use of them? Why do we use @property and @synthesize?

  • 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-03T01:56:02+00:00Added an answer on June 3, 2026 at 1:56 am

    Getter is a method which gets called every time you access (read value from) a property (declared with @property). Whatever that method returns is considered that property’s value:

    @property int someNumber;
    

    …

    - (int)someNumber {
        return 42;
    }
    

    …

    NSLog("value = %d", anObject.someNumber); // prints "value = 42"
    

    Setter is a method which gets called every time property value is changed.

    - (void)setSomeNumber: (int)newValue { // By naming convention, setter for `someValue` should 
                                        // be called `setSomeValue`. This is important!
        NSLog("someValue has been assigned a new value: %d", newValue);
    }
    

    …

    anObject.someNumber = 19; // prints "someValue has been assigned a new value: 19"
    

    Usually it doesn’t make much sense to just return the same value from getter and print new value in setter. To actually store something you have to declare an instance variable (ivar) in your class:

    @interface SomeClass : NSObject {
        int _someNumber;
    }
    

    and make accessors (the collective name for getters and setters) to store/retrieve it’s value:

    - (int)someNumber {
        return _someNumber;
    }
    
    - (void)setSomeNumber:(int)newValue {
        _someNumber = newValue;
    }
    

    …

    SomeClass *anObject = [[SomeClass alloc]init];
    anObject.someNumber = 15;
    NSLog(@"It's %d", anObject.someNumber); // prints "It's 15"
    

    Okay, now that property behaves just like the usual variable. What’s the point in writing all that code?

    First, from now on you can add some extra code to the accessors, which will get executed each time the property is accessed or changed. There are multiple reasons for doing that, for example I may want to do some kind of hidden calculations, or updating my object’s state, caching stuff etc.

    Second, there are cool mechanisms called Key-Value Coding (KVC) and Key-Value Observing (KVO) in Cocoa. They depend on properties. You can read about them in the Developer Library: KVC Programming Guide and KVO Programming Guide. Those are advanced topics though.

    Last, in Objective C there is no static allocation for objects. All the objects are dynamically allocated (reason). If you want to keep your object pointers in instance variables (as opposed to properties) you will have to do all the memory management manually every time you assign new value to your ivar (not true when Automatic Reference Counting is on). Using properties you could put some memory management code in the accessors and make your life easier.

    I don’t believe this explanation will make much sense to someone who is not familiar with Objective C memory management, so, either read some real docs/tutorials on it, or just use properties (instead of instance variables) until you learn all the details one way or another. Personally, I don’t like the second option, but it’s up to you.

    You can use @synthesize to make the compiler generate basic accessors and underlying instance variables for you automatically. Instead of the code above (-(int)someNumber and -(void)setSomeNumber:) you could just write

    @synthesize someNumber = _someNumber; // = _someNumbers tells compiler 
                                          // to name the instance variable `_someNumber`. 
                                          // You could replace it with = `_somethingElse`, of
                                          // course, but that's an ill idea.
    

    This single line generates int _someNumber variable, someNumber getter and setSomeNumber setter for you. If you want the accessors to do anything more complex than just store/retrieve the value from some instance variable, you will have to write them yourself.

    Hope all this makes any sense.

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

Sidebar

Related Questions

Possible Duplicate: Why use getters and setters? I have read books on Java, saying
Possible Duplicate: Why use getters and setters? In C#(ASP.NET) we use getter and setter
Possible Duplicate: Why use getters and setters? Yes, It's a very simple thing but
Possible Duplicate: Why use getters and setters? I see this a fair bit in
Possible Duplicate: Is it really that wrong not using setters and getters? Why use
Possible Duplicate: Why use getters and setters? I'm reading the Java for Dummies 2nd
Possible Duplicate: Public Data members vs Getters, Setters In what cases should public fields
Possible Duplicate: iOS - Detecting whether or not device support phone calls? I'm writing
Possible Duplicates: Public Data members vs Getters, Setters Purpose of private members in a
Possible Duplicate: WPF Button isPressed and isEnabled problem Here is a snippet of code

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.