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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:55:13+00:00 2026-06-12T12:55:13+00:00

Ok, so I was reading some articles regarding Good Programming Practices and I came

  • 0

Ok, so I was reading some articles regarding Good Programming Practices and I came across a statement which said that making all your Elements public for your classes isn’t a good idea.. aka The concept of Encapsulation.

Now in Objective C, When I create a element for my class, I do the following, consider an NSMutableArray

@property (nonatomic, retain) NSMutableArray* myArray;

WHY I DO THIS?

So as to give the Retain property to myArray and therefore, giving it a simpler Memory Management cycle. Later on, I initialize the myArray in viewDidLoad as

self.myArray = [NSMutableArray arrayWithCapacity:0];

Later in Dealloc…

self.myArray = nil;

WHAT ELSE HAPPENS

By giving this property and synthesizing myArray in .m file, what I am unknowingly doing is making Public Getters and Setters for all the elements of my class.

Also, the auto-generated UI Elements from Xib files, do have the same declarations applied.

That isn’t a nice idea to keep creating public Getters and setters for each and every element of your class, right?

So, there’s absolutely no kind of encapsulation applied! Please correct me if I am wrong here and help me with any solutions!

Thanks!

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

    Simply use the principle of Class Extensions that allows you to put part of your declarations in your .m file, thus making it invisible in your header and invisible from other classes.

    (This is Apple’s recommended way to declare private methods and properties, by the way)

    YourClass.h

    @interface MyClass : MySuperclass
    // public properties
    // public methods
    @end
    

    YourClass.m

    @interface MyClass()
    // This is a class extension
    // put here private properties
    // and private methods too
    @end
    
    @implementation MyClass
    // And your implementation of course here
    @end
    

    You can even declare a property as readonly in your public interface (in the header file) and redeclare it as readwrite in your private interface (in the class extension in the .m file) for example.

    See the Apple documentation for more details.


    Note that:

    • There is NO NEED to declare the instance variable if you declare the property: the compiler will generate it automatically for you so you don’t have to bother and to declare it in the .h. In the latest version of the compiler (Modern Objective-C) there is even no need for the @synthesize directive as it now generate it automatically if not present (see doc)
    • If you prefer to declare instance variables anyway, you can also do this in the class extension in your .m the same way you would do in your .h. That’s a way to hide instance variables from the public header too. In general I really rarely use instance variables (as declaring only the properties is sufficient) and if I really need an ivar I declare it in the class extension to make it not visibile in the public header.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While reading some articles about writing web servers using Twisted , I came across
From my reading of some android forensic articles i understand that sqlite records can
Reading some articles, told me that soft keys varies between devices. Some says -6
I've been reading up on the Factory pattern, and have come across articles that
I was (re)reading Joel's great article on Unicode and came across this paragraph, which
After reading some articles about XSS I have incorporated HTMLPurifier into my zend framework
I've trying to use the API for CapsuleCRM... http://capsulecrm.com/help/page/api_gettingstarted I've been reading some articles
What is the difference between XML and SOAP? I've been reading some introductory articles
I was just reading this article and it mentions that some organization had an
I'm in the process of learning Interfaces. I've been reading through some books/articles and

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.