PDA

View Full Version : Classes




Nnavick
Oct 19, 2010, 03:53 PM
Hi,
I wrote a class that calculate point ,

the .M file of the class:

#import "GameBrain.h"


@implementation GameBrain


-(int)CalculatePoint:(int)ArrowPoint,int NewPoint {
if(ArrowPoint>NewPoint) {
NewArrowPoint=ArrowPoint-NewPoint;
NewArrowPoint*=-1;
}
else
NewArrowPoint=NewPoint-ArrowPoint;

return NewArrowPoint;
}


@end

the .H file of the class:

#import <Foundation/Foundation.h>


@interface GameBrain : NSObject {

int NewArrowPoint;
}



-(int)CalculatePoint:(int) ArrowPoint,int NewPoint;
@end

How do I call the class and the method "CalculatePoint" from the main program ?

Thanx



dejo
Oct 19, 2010, 06:35 PM
-(int)CalculatePoint:(int) ArrowPoint,int NewPoint;
First, are you sure you want to be using unconventional syntax like this?

How do I call the class and the method "CalculatePoint" from the main program ?
Second, you don't "call a class". A class is simply a definition that you can use to create an object of its type, a process known as instantiation.

Nnavick
Oct 19, 2010, 11:44 PM
First, are you sure you want to be using unconventional syntax like this?


Second, you don't "call a class". A class is simply a definition that you can use to create an object of its type, a process known as instantiation.

By creating an object you mean ,
GameBrain * game;
what next? when I write [game and then escape i can't see the methods of the class]

?

dejo
Oct 20, 2010, 12:07 AM
By creating an object you mean ,
GameBrain * game;

No, that just declares an object (and, technically, it just declares a pointer to that object). You haven't instantiated it yet.

Nnavick
Oct 20, 2010, 07:17 AM
So how do I initialize the class?

robbieduncan
Oct 20, 2010, 07:42 AM
So how do I initialize the class?

I think, given this question, that you need to stop writing code and learn the basics of the language. This really is about as basic as it gets.

Edit to add: you should read, and understand, all of this document (http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/ObjectiveC/Introduction/introObjectiveC.html). If you don't understand something don't skip over it. Stop, think about it, re-read until you are sure you understand what is going on.