chizzer2003

macrumors member
Original poster
Oct 2, 2010
82
0
Hello, I am very confused on the concepts of class, methods and instances. I have honestly looked through my objective-c book and searched the web for hours but no avail. Any help would be greatly appreciated. I have this very simple piece of code from my book and it would greatly help if one of you could explain what bits are the objects, methods and instances. Thankyou.

Code:
@interface Fraction: NSObject
{
int numerator;
Int denominator;
}
 
- (void) print;
- (void) setNumerator: (int) n;
- (void) setDenominator: (int) d;

@end
 

jnoxx

macrumors 65816
Dec 29, 2010
1,343
0
Aartselaar // Antwerp // Belgium
.. This is explained in nearly every book :eek:
But because i'm in a good mood i'll show you around

Code:
// Here you declare the name of your CLASS, which is a subclass of //NSObject, which is an standard objective-C (& Cocoa Touch class)
@interface Fraction: NSObject
{
/* Here you declare your ivars (instance variables), with a int as object, and numerator as your name (and you have Int instead of int, something wrong here..)*/ 
int numerator;
Int denominator;
}
 /* Here you define your public methods, if you import this class in another class, these methods will be shown public, and can be accessed, these should not be mistaking with Static methods */
/* The void is the return type, here void(nothing), and (int)n are the parameters the methods wants to receive */
- (void) print;
- (void) setNumerator: (int) n;
- (void) setDenominator: (int) d;

@end

Not sure if it's 100% correct, I never went to high school or anything, so learned everything myself.
 

chizzer2003

macrumors member
Original poster
Oct 2, 2010
82
0
Thankyou so much, this really helps! Will defo get me on my way, just couldn't get my head around it. Cheers! :)
 

jnoxx

macrumors 65816
Dec 29, 2010
1,343
0
Aartselaar // Antwerp // Belgium
I'm sorry, but if this is hard, and you oversaw that in a book >.> what did you pick up (no personal harm, just a question)
This is just OOP, but in a different syntax.
But no problem after all :)
 

chizzer2003

macrumors member
Original poster
Oct 2, 2010
82
0
Programming in Objective-C - Stephen G. Kochan.

It was just hard for me to get my head around thats all, he used some rather odd analogies.
 

jonnymo5

macrumors 6502
Jan 21, 2008
279
0
Texas
I always found it best to think of classes like real world objects. Think of a coffee mug as an object.

Mug - is your class

You have some instance variables like color, size, emotional attachment.
You have some methods like fillMug, emptyMug, breakMug.

Code:
@interface Mug: NSObject
{
Color color;
int size;
int emotionalAttachment;
}
- (void) initWithColor:(Color *)color andSize:(int)size andEmotionalAttachment:(int)emotionalAttachment
- (void) fillMug;
- (void) emptyMug;
- (void) breakMug;

@end
Each instance of your Mug class will have the same exact actions or methods you can perform on it but most of them will have different values in the instance variables.

Now you can create all your Mugs.
Code:
Mug *worldsBestDad = [[Mug alloc] initWithColor:white andSize:large andEmotionalAttachment:priceless];
Mug *anotherCatMug = [[Mug alloc] initWithColor:pink andSize:enormous andEmotionalAttachment:expendable];

And call your methods.
Code:
[worldsBestDad fillMug];
[anotherCatMug breakMug];

*excuse any broken syntax, the forum compiler is not working :D
 

chizzer2003

macrumors member
Original poster
Oct 2, 2010
82
0
thanks a lot guys! :) set me on the path to actually be able to do something useful with computers rather than just recklessly mess around with them haha ;)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.