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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:52:02+00:00 2026-06-09T17:52:02+00:00

I’m still new to Objective-C and I recently learned how to make properties, so

  • 0

I’m still new to Objective-C and I recently learned how to make properties, so far so good, but one thing that bothers me is that the setter and getter methods are still publicly available even after the property is made.

let’s say I have the following code:

// myClass.h
@interface myClass : NSObject {
int _startPos;
}
@property (assign, readwrite, setter = setStartPos:, getter = getStartPos) int startPos;
-(void) setStartPos: (int) pos;
-(int) getStartPos;
@end

the implementation file should be rather self-explanatory, I’m not trying to do anything specific.

Now, I read somewhere, and tried it in practice that if you make a category in the implementation file, and add some methods to that, it’s possible to make those methods invisible (aka private) to things outside of the myClass.m file.

“Alright” I think, and decide to try it out:

//myClass.m
#import <Foundation/Foundation.h>
#import "myClass.h"

@interface myClass (hidden)
-(void) setHiddenStartPos: (int) hPos;
-(int) getHiddenStartPos;
@end

@implementation myClass (hidden)
-(void) setHiddenStartPos: (int) hPos {
_startPos = hPos;
}

-(int) getHiddenStartPos {
return _startPos;
}
@end

@implementation myClass
-(void) setStartPos: (int) Pos {
[self setHiddenStartPos: Pos];
}

-(int) getStartPos {
return [self getHiddenStartPos]; //this is to see if I can get the value from the hidden methods through the non-hidden ones
}
@end

that’s all fine, and testing it in main() I can see that the methods with “hidden” in their name are in fact inaccessible, and therefore act as if they are private.

Then I tried to add this to the header file:

@property (assign, readwrite, setter = setHiddenStartPos:, getter = getHiddenStartPos) int

to see if I could access the hidden methods through the property
but when I did that, the hidden methods became accessible in main() and the whole plan with making the methods only accessible through the property went down the drain

So I ask you, is there a way to make methods inaccessible to anything BUT the property and/or the object itself?

Edit: I realize that getters don’t usually have get in the name, so please stop commenting on it?

also to emphasise what I meant:
I wanted to make properties like in c#, where the content of the setters and getters are private to the property itself

public int exampleProperty
{
    set{...}
    get{...}
}

it doesn’t use methods as getters and setters, and therefore the code in the setters and getters are accessible to only the property, JUST like the code within a method is local to the method itself

  • 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-09T17:52:04+00:00Added an answer on June 9, 2026 at 5:52 pm

    You’re over-thinking what “private” means. In ObjC, “private” just means “not visible.” It doesn’t mean “not callable.” The way you make a method private is to not put it in your .h file, which declares your public interface.

    There is no way to control who passes a message. This is a key fact of ObjC and cannot (and should not) be changed. ObjC is a dynamic language. At runtime, I am free to generate selectors and call performSelector: on any object I want. Anything that stopped that would either (a) introduce significant performance penalties, or (b) break many very useful and common techniques in ObjC (probably both). ObjC is not Java or C#. It’s not even C or C++. It’s Smalltalk on top of C. It’s a highly dynamic language and that has a lot of strengths. Unlearning other languages is the first step towards becoming a good Cocoa developer.

    It would be nice to have a compiler-checked @private for methods (of which properties are just a special case), and it would especially be awesome to have a compiler-checked @protected for methods (these exist for ivars). These would make it slightly simpler to avoid some kinds of mistakes. But that’s the only way you should be thinking about this. The goal is not to protect one part of the code from another part of the code. The other code is not the enemy. It’s all written by people who want the program to work. The goal is to avoid mistakes. Correct naming, consistency, and the absolute elimination of warnings is how you achieve that in ObjC.

    So yes, I’d love to be able to put @protected in front of my @property declarations occasionally. Today you can’t, and there is no real equivalent (I sometimes use a +Protected category in a separate header, but it’s generally more trouble than its worth). But that said, having it wouldn’t change very much, and I only find a case where I would even use this a few times a year. I can’t think of single case where @private for a method would have been really useful, though.

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

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
I want use html5's new tag to play a wav file (currently only supported

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.