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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:28:54+00:00 2026-06-12T02:28:54+00:00

New to Objective-C, and i am basically from c++ background. I am learning now

  • 0

New to Objective-C, and i am basically from c++ background. I am learning now objective-c and would like to get confirmation of what i understood is write or wrong?. Kindly advise.

I have the following class:

@interface Test: NSObject
{
 int instance1; 
}
@property int instance1;
 - (void)  sayHello;
@end

Class ‘Test’ has a instance variable instance1. If the member function ie: sayHello wants to access the variable, it has to happen through getter/setter functions. So, There are two ways to get it :

  1. User can define.
  2. We can get the help from the compiler?. How?.
    declare the same variable as a property, and synthesize it, the the compiler
    gets the code of getter/setter for us for that particular variable.

So, Untimately, getter/setter is the only way to access the variable in the method implementation, ie. both self.instance1 = 100; and instance1 = 100 need getter/setter.

Having missed both 1. and 2., there is no way to access the instance1 variable.

Also, instance1 is a pubic variable can can be accessed outside of the class with object instance.

Test *t = [[ Test alloc] init];
t.instance1 = 200;

Questions:

  1. Is there any way to make instance1 is “private”, so that I can not access the instance
    variable outside the class?
  2. Is there anything wrong in my understanding?
  • 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-12T02:28:55+00:00Added an answer on June 12, 2026 at 2:28 am

    If the member function ie: sayHello wants to access the variable, it has to happen through getter/setter functions.

    It doesn’t have to. You can access ivars directly, without using accessor methods:

    - (void)sayHello {
       instance1 = 123;
    }
    

    You can define private ivars by declaring them in the implementation file, not the header:

    @implementation Test {
       int privateVar;
    }
    // ... additional implementation, methods etc.
    @end
    

    Note, that since Xcode 4.4 you don’t have to declare your ivars anymore. You simply declare a property. The ivar and the accessor methods will be synthessized automatically.

    For more details, I recommend reading my answer to this question: Declaration of variables

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

Sidebar

Related Questions

I'm new in objective-c, and I'm now trying to write a simple user-sign-in program
i'm kinda new to Objective-C so i have been learning with the Head First
So new to objective-c and iphone/ipad development. Trying to get my feet wet with
I would like to know how I can import a library into Objective-C at
I am currently learning Objective-C, one step at a time! Right now, my main
I am making a new Objective C class and it gave two options as
I am new to iOS development. I've recently stumbled upon the new objective-c collection-literals
New to objective-C, #import <objc/objc.h> #import <Foundation/Foundation.h> @interface Test:NSObject { int x,y, abc; NSString
Fairly new to Objective C and trying to work within Storyboards given it's the
I'm very new to objective C. I followed the tutorial for integrating Facebook to

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.