Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I make methods only available to properties in Obj-C?
    text
    copied!<p>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. <br /> let's say I have the following code:</p> <pre><code>// myClass.h @interface myClass : NSObject { int _startPos; } @property (assign, readwrite, setter = setStartPos:, getter = getStartPos) int startPos; -(void) setStartPos: (int) pos; -(int) getStartPos; @end </code></pre> <p>the implementation file should be rather self-explanatory, I'm not trying to do anything specific. <br /> Now, I read somewhere, and tried it in practice that if you make a <em>category</em> in the implementation file, and add some methods to that, it's possible to make those methods invisible (aka <em>private</em>) to things outside of the myClass.m file.</p> <p>"Alright" I think, and decide to try it out:</p> <pre><code>//myClass.m #import &lt;Foundation/Foundation.h&gt; #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 </code></pre> <p>that's all fine, and testing it in <em>main()</em> I can see that the methods with "hidden" in their name are in fact inaccessible, and therefore act as if they are private.</p> <p>Then I tried to add this to the header file:</p> <pre><code>@property (assign, readwrite, setter = setHiddenStartPos:, getter = getHiddenStartPos) int </code></pre> <p>to see if I could access the hidden methods through the property <br /> but when I did that, the hidden methods became accessible in <em>main()</em> and the whole plan with making the methods only accessible through the property went down the drain</p> <p>So I ask you, is there a way to make methods inaccessible to anything BUT the property and/or the object itself?</p> <p><strong>Edit:</strong> I realize that getters don't usually have get in the name, so please stop commenting on it?</p> <p>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</p> <pre><code>public int exampleProperty { set{...} get{...} } </code></pre> <p>it doesn't use methods as getters and setters, and therefore the code in the setters and getters are accessible to only the property, <strong>JUST</strong> like the code within a method is local to the method itself</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload