Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
August 18, 2018, 09:25:57 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
 
   Home   Help Search Login Register Chat  
Pages: [1]   Go Down
  Print  
Author Topic: The 'private' aspect of methods in class extensions  (Read 1016 times)
jonr
Full Member
***
Posts: 155


« on: November 30, 2014, 09:29:22 PM »

I was confused by this in how the book described this, but can see how it works by trying the example myself.  In the example, we put remove the reduce method from the Fraction.h @interface section and instead put it in @interface Fraction () in the Fraction.m file.  The reduce method is a method that is called by the other methods.  I can certainly see how making it private via the class extension functionality keeps the reduce method by being accessed directly, while still allowing it to be used by the other methods.  In other words, a Fraction object could not use reduce directly but the other methods can still have reduce (nested) inside.

With that said, and if my observations are correct, I'm not sure I understand the choice of words / method of explanation that the book used.  In the second paragraph in the 'Class Extensions' section in chap. 11, it is written:
Quote
Class extensions are useful, because their methods are private. So, if you need to write a class that has data and methods that can be used only within the class itself, as class extension might fit the bill.
The second sentence in bold seems confusing to me, specifically 'only within the class itself.'  What does that mean exactly?  In the example with the reduce method, having the method be part of a class extension makes it unavailable to class instances directly; the method is now only available to other methods.  Isn't a class instance within the class?  So, therefore isn't the explanation misleading and unclear?  At first I was thinking that putting the method in a class extension meant that class instances of sub-classes couldn't access the method and this was what happens when you place a method in a class extension, but class instances have the same limitation.

It wasn't until the next paragraph (after the note section) where it explicitly says
Quote
'...only want the methods defined in the implementation section of the Fraction class to have access....
did it become clear.

Am I incorrect in how I interpreted 'within the class itself'?  OR  was I really off-base since that description/phrase typically does not include class instances since those instances are not part of the class definitions (*h and *m files).
Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents ゥ 2009 classroomM.com. All rights reserved.