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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:42:18+00:00 2026-06-10T12:42:18+00:00

I came from C++/Java world where it’s quite obvious how to create private members.

  • 0

I came from C++/Java world where it’s quite obvious how to create private members. However, I saw several ways to do this in Objective C and I would like to hear cons and pros

1) Declare them as @private in .h file

@interface MyClass : NSObject
{
    @private
    int someMember;  
}
@end

2) Declare them in interface inside .m files

@interface MyClass() {
    int someMember;  
}
@end

@implementation MyClass
@end

3) Declare them in implementation

@implementation MyClass {
     int someMember;  
}
@end 

What is the preferred method and why? Did I miss any other methods?

  • 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-10T12:42:20+00:00Added an answer on June 10, 2026 at 12:42 pm

    My preference is #3:

    // MyClass.m
    ...
    @implementation MyClass {
         int someMember;
    }
    @end
    

    It allows you to cleanly abstract well and minimize dependencies.

    Since every objc object is allocated dynamically and there is little physical dependence, you can have the fastest build times and the richest object representation minimal compilation overhead (e.g. minimal #imports and physical dependence).

    Abstraction is also a huge plus — private is the default, and there is little reason you should deviate from that, and little reason to expose the internals to others. This hides it all very nicely while ensuring type safety is not compromised. Big plus: you can also declare C++ values in your object easily without exposing everyone to all your C++ libraries’ dependencies — not PIMPL, not a struct in the global scope, not a *void**, but a proper value. it’s a great compilation firewall. If you have worked on large C or C++ projects, you may rejoice.

    Of course, declaring ivars as seen in option #2 brings all this. So it pretty much depends on where and how you prefer to see your variables. An ivar declaration is concrete, whereas a property may be abstracted — so i favor grouping concrete with concrete and interface with interface but either way; #2 or #3 are ideal, unless you need backwards compatibility.

    If you want (pseudo-)private properties, I recommend you declare them in the class continuation:

    // MyClass.m
    ...
    @interface MONClass ()
    @property (nonatomic, copy) NSString * string;
    @end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I came from Java to C++ ... When I try to do this ...
I came from a C-Java-C# world... where the parentheses were VIP citizens and types
Background I came from several years working in a company where all the database
There's this already populated database which came from another dev. I'm not sure what
I am using this code to check if the request came from a page
I have MS Visual Studio web project folder came from TFS server with several
I came from a java background, and I was trying to use protocol like
I have came from another question to this one: How can one start with
I came across this in an java interview earlier. It was one of the
As someone who came from Java background and being a newbie to Ruby, I

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.