PDA

View Full Version : Objective C beginer question




larswik
Jun 23, 2010, 04:18 PM
Have limited Python programming experience and started learning Objective C and have a couple questions before I move on in the book 'Prog. in Obj-C'.

I understand purpose of Objects and instances of Objects. I need help with part of this.
@interface Fraction: NSObject
{
int numerator;
int denominator;
}
-(void) print;
-(void) setNumerator: (int) n;
-(void) setDenominator: (int) d;

@end


The first line creates the Object named Fraction - I get that.
The Object has 2 variables of type integer - I get that.
I read and understand the next part with the -(void) setNumerator: (int) n; and how all that works, but what I don't get is why that code is in the Object? These come up again in the next part of the code called '@implementation Fraction'. Are these 3 -(VOID) lines kind of like doorways to pass the information to and from instance Objects?

I hope I was not to confusing in trying to explain this.

thanks!



dejo
Jun 23, 2010, 04:23 PM
The first line creates the Object named Fraction - I get that.
Actually that line defines a class named Fraction; it doesn't create an object (or instance) of that class.

larswik
Jun 23, 2010, 05:15 PM
Sorry that is what I meant. It defines not creates. The 'init' and 'alloc' reserves the memory and creates the instance if I read correctly.

-Lars

dejo
Jun 23, 2010, 05:27 PM
Have you read "The @interface Section" yet? Also, you may want to go back and re-read the "Instances and Methods" section from earlier in the chapter.

Trickerie
Jun 23, 2010, 05:44 PM
I guess the easiest way to explain it, is saying:

Class Fraction is defined.

Each object of class Fraction has 2 int variables, and 3 void methods.

dejo
Jun 23, 2010, 05:53 PM
Each object of class Fraction has 2 int variables, and 3 void methods.
I would phrase it: 2 instance variables of type int and 3 instance methods with void return types.

larswik
Jun 24, 2010, 01:37 AM
I did read the @interface Section starting page 33 and that is where I started to get lost. The first couple tutorial before this section I understood with no problem. There was a lot to take in starting at the @interface section for a few pages.

I think I get the 3 Voids now from your last reply. They are the instance methods for the class Fraction that I am defining. Exmpl "Fill the car, Wash the car, drive the car." I missed the part that the VOIDS are the instance methods and they need to be there.

Let me make sure I understand with a couple questions.
1. If I define a Class with NO instance methods it won't work because it can't transfer values to it to be stored in the variables.

2. In order for the int numerator; variable in the Class that I am defining to store a value I have to have the -(void) setNumerator: (int) n; which will pass the argument to the int numerator;..... If the -(void) setNumerator: (int) n;
did not exist as the instance method then I could not pass a value to the class that I am defining.

Am I correct in understanding that?

Thanks again. I want to wrap my head around this section before reading on in the book.

PhoneyDeveloper
Jun 24, 2010, 07:50 AM
Just to be pedantic the @interface block DECLARES a class. The DEFINITION is the source code for that class, which goes in the @implementation block.

Two of the methods in this class declaration are setters. They serve to set properties of the instances of this class.

Your comments about "won't work" and "could not" are strictly speaking not correct. I'm not sure if the exceptions to your statements are too important for a beginner to understand. These setters are meant to set ivars.

However not all setters need to set an ivar. Imagine a class that represents a database. It could have setters where the values go into the database, not into ivars. Imagine a class that stores all its values in a container object, like a dictionary or an array. Its setters could modify the container but not directly save to an ivar. There are also other ways to set ivars than directly through setters.

Suffice it to say that these particular setters serve the purpose of setting these ivars. This is certainly a common way of doing things.

smb
Jun 24, 2010, 08:30 AM
larswik,

You might want to check out the forum for this book. Steve Kochan usually posts answers to questions there about the book: http://classroomm.com/objective-c/

larswik
Jun 24, 2010, 11:39 AM
Thanks for the link and additional info. I reread the section last night again and I'm starting to get the 3 different sections.

Thanks!

NickFalk
Jun 25, 2010, 03:46 AM
Don't know if you've got it sorted out yet, so just in case:
-(void) print; - Defines a method that's called print. (void) simply tells the compiler that the method will not return any variable or object, as opposed to for instance: -(int) print; which would be expected to return an integer.