Amazon.com Widgets Why do we put the origin ivar in the implementation and not the interface?
Welcome, Guest. Please login or register.
Did you miss your activation email?
October 21, 2014, 09:46:09 PM
Home Help Search chat Login Register 
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+ Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+ Programming in Objective-C, 4th edition
| |-+ Chapter 8
| | |-+ Why do we put the origin ivar in the implementation and not the interface?
Pages: [1] Go Down
Print
Author Topic: Why do we put the origin ivar in the implementation and not the interface? (Read 999 times)
CloudDweller
Newbie
*
Posts: 17






on: March 11, 2012, 06:48:38 AM

Is there a difference between putting the following in the implementation file and not the interface?

Code: (Objective-C)
{
    XYPoint *origin;
}

In previous chapters we've declared instance variables in the interface file but in program 8.4 we declare it in the implementation file and I wondered what the difference is?
Logged
apexmutt
Newbie
*
Posts: 12


Email




Reply #1 on: March 11, 2012, 08:05:44 AM

Not sure how if you have the 4th edition, but in my 3rd edition, it's showing it in the interface file.

But just to answer the question, it doesn't seem like there is a difference as I've declared an instance variable in both the implementation and interface section, set them as a property in the interface, synthesized them in the implementation and everything seems to be working fine...

I'm actually curious about this as well and am hoping someone with more knowledge of this language can explain. *cough cough* skochan...  Grin
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #2 on: March 12, 2012, 03:00:03 PM

Declaring instance variables in the implementation section is a new feature added to the Apple LLVM compiler and released with Xcode 4.2, which is why it's not mentioned in earlier editions.

If the instance variable is declared in the interface section, then by default, it can be referenced by name by subclasses.  If it's declared in the implementation section, it's private and cannot be accessed directly by name in a subclass.   The preferred way of accessing instance variables in subclasses is through properties or explicitly-coded getter/setter methods.   The synthesized methods for a property (i.e., the setters and getters) and/or the setter/getter methods declared in the interface section are accessible by subclasses, or by anyone else using the class.   These represent the "public" means of access to the class' data.

Cheers,

Steve
Last Edit: March 12, 2012, 04:02:22 PM by skochan Logged
Pages: [1] Go Up
Print
Jump to:



Login with username, password and session length

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 (c) 2009 classroomM.com. All rights reserved.