Amazon.com Widgets Program 4.6 confusion
Welcome, Guest. Please login or register.
Did you miss your activation email?
August 31, 2014, 01:32:25 AM
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 4
| | |-+ Program 4.6 confusion
Pages: [1] Go Down
Print
Author Topic: Program 4.6 confusion (Read 2010 times)
LochAwe
Newbie
*
Posts: 9






on: March 28, 2012, 07:49:59 AM

I'm coming back to this book after a 3-month absence, so I started over and have been taking a lot of notes, and I thought I was understanding things well until I got here:

=======
Code: (Objective-C)
@interface Calculator : NSObject

// accumulator methods

-(void) setAccumulator: (double) value;
-(void) clear;
-(double) accumulator;

// arithmetic methods

-(double) add: (double) value;
-(double) subtract: (double) value;
-(double) multiply: (double) value;
-(double) divide: (double) value;

@end

// ---- @@ implementation section ---

@implementation Calculator
{
 double accumulator;
}

======
The text says that the method definitions are quite straightforward, but it isn't straightforward to me, unfortunately. I am confused by having "(double) accumulator" in both the @interface and @implementation sections.

My understanding from the text is that the @interface section is used to define the new class, name its parent class, and define methods to be used with it, and that the memberDeclarations part of the @implementation section is used to declare the instance variables as well as the code for carrying out the methods.

I can see where the instance variable accumulator has been defined in the @implementation section, but I am very confused by its presence in the @interface section as well, where I'd expect to see only methods.

Or is there a method called accumulator as well as an instance variable called the same thing?

Sorry to be to thick, but I feel as if I'm missing something basic here.


Logged
seerex
Full Member
***
Posts: 177


Email




Reply #1 on: March 28, 2012, 07:55:39 AM

You can also have variables as part of the interface files, which is places in brackets below the class name (or as properties, but you'll learn about that later). So you can add variables like this:

Code: (Objective-C)
@interface Calculator : NSObject  
{
   int myInt;
   int anotherIne;
}

Now, these are whats called "Instance variables", and each instance of my object has these 2 ints. I can manipulate them in the methods of the implementation section, as they know about their existence.

I'm not exactly sure what ur confused about, so be a little more specific if this didn't answer it Smiley
Logged

Best Regards -
J.B.J
If you think i helped you could return the favor Wink

Subscribe to my channel on youtube: http://www.youtube.com/user/JBJProgramming

My apps:
- iTap Fruits (all countries)
- Toilet-Quiz (Denmark only)
- Zoo Kids (Denmark only)
- Diablo Item Database (All countries)
LochAwe
Newbie
*
Posts: 9






Reply #2 on: March 28, 2012, 08:05:50 AM


Hi,

Thanks for your response. I'm confused as to why it's there twice in Program 4.6, in both the @interface and the @implementation sections.  Smiley


Logged
LochAwe
Newbie
*
Posts: 9






Reply #3 on: March 29, 2012, 03:05:49 PM

I have been looking at this some more, and I think my confusion also relates to where the instance variables are declared. Chapter 3 says that instance variables are declared in the @implementation section (as memberDeclarations.) However, it also seems to allude to them being declared in the @interface section, where it says the general format is propertyAndMethodDeclarations.

When you start the @interface section in XCode, it seems to expect that the instance variables will be declared there, because it gives this sample format:

@interface classname: superclass
{
 instance variables;
}
@end

So I don't understand why the book says they're declared in the @implementation section, and I think that's what's really got me confused about Program 4.6.

Are instance variables declared twice, in both the @interface and @implementation sections?
Logged
LochAwe
Newbie
*
Posts: 9






Reply #4 on: March 29, 2012, 03:55:30 PM

AH. Okay... in program 4.6, that's a "getter" method being declared in the @interface section, not an instance variable, isn't it? Both the getter method and the instance variable have the same name. God, that's confusing, but maybe I'll wrap my head around it eventually. Is that part of "thinking like a programmer?"

I'm still confused about whether instance variables get declared in @interface or @implementation, though, because of the way xCode's auto-complete seems to expect them in @interface.
Logged
Bunchadna
Newbie
*
Posts: 27






Reply #5 on: April 14, 2012, 05:21:59 PM

LochAwe, thanks for posting this - I'm experiencing the same confusion, and went through the exact same thought process as you.  I still don't quite get it - but I'll just chug along until the Aha! moment arrives ... oh how I long for the Aha! moment ...  Grin
Logged
JackRabbit
Jr. Member
**
Posts: 54



Email




Reply #6 on: April 16, 2012, 11:10:40 PM

Greetings LochAwe

Quote
AH. Okay... in program 4.6, that's a "getter" method being declared in the @interface section, not an instance variable, isn't it? Both the getter method and the instance variable have the same name. God, that's confusing, but maybe I'll wrap my head around it eventually. Is that part of "thinking like a programmer?"

When I read your posts, I suddenly became confused too. (It's catching, like the flu.) But as I went with you though your posts, I lost my confusion.

Yes, the instance variable and the getter method have the same name. They usually, if not always do. We can tell the differences by the dash "-" and backers "()" for the methods and the curly brackets for the instance variables. So:

Method
Code: (Objective-C)
-(double) accumulator;

Instance Variable
Code: (Objective-C)
    {
        double accumulator;
    }

seerex and I got into a discussion about getter method & variable names not too long ago (see  Chapter 3 - Naming Setter and Getter Methods.) where I was wondering why we Apple frowned on the use of the prefix "get" in front of the getter method. After all, setter methods begin with the prefix "set". Maybe that discussion will be of value to you, or....maybe it won't.

The question of where do the instance variables go used to confuse me. It seems that older Xcode's, before the 4.0 series asked for the instance variables to go in the interface file (.h). After 4.0, Apple wanted the instance variables to go into the implementation file (.m), which encapsulates the data keeping it safe from the prying eyes of the users of that object.

Any way LochAwe, thank you for bring this point up, reading your musings on this thread made the topic of getter methods and the instance variables they deal with clearer.

JR
Last Edit: April 17, 2012, 12:40:07 AM by JackRabbit Logged
rue
Jr. Member
**
Posts: 53






Reply #7 on: April 22, 2012, 10:36:34 AM

You know what, I also thought variables should be in the @interface section. So I tried moving it in the @implementation section and I'm surprised, it still works.

Quote
I was wondering why we Apple frowned on the use of the prefix "get" in front of the getter method.
I also thought "set" and "get" were the normal convention with methods.

But I think Apple introduced the dot notation? so the "get" kinda gets in the way of readability if used in dot notation? Just guessing.

x=object.getVariable   same as   x = [object getVariable]

x=object.variable is more readable, me thinks than  x=object.getVariable




Logged
JackRabbit
Jr. Member
**
Posts: 54



Email




Reply #8 on: April 29, 2012, 01:10:54 AM

Greetings rue.

You guess on the getter method label and dot notation might be a good one. (I wish Steve would put his 2 cents worth here at this point.)

But the most important thing we must understand is that the variables must go into the implementation section putting them out of reach of the future user of that class. If we don't and put it into the interface section (or header file) the data is not encapsulated, which means that it can be seen and changed by users of that class. This would negate one of the major features of Object Oriented Programming.

JR
Logged
rue
Jr. Member
**
Posts: 53






Reply #9 on: April 29, 2012, 05:34:44 PM

Quote
But the most important thing we must understand is that the variables must go into the implementation section putting them out of reach of the future user of that class. If we don't and put it into the interface section (or header file) the data is not encapsulated, which means that it can be seen and changed by users of that class. This would negate one of the major features of Object Oriented Programming.

Yeah, I think Apple finally allowed that variables be put in the @implementation. Originally, (and if you have some older Apple Objective C PDFs), you can see they first required the variables to be in the @interface section. Later on, that changed to what is now the current recommendation.
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.