On Jan 10, 2009, at 5:19 PM, Angel Manuel Mariblanca Gonzalez wrote:
I'm new developing for Mac,
Welcome!
I'm starting, and I've got a problem with methods and class.
The xcode-users list is for questions related to Apple's developer tools, not for language questions. I've put some answers below to get you started, but this list is not the place for an extended discussion of these issues.
For framework questions, you can ask on the cocoa-dev mailing list, also hosted by Apple.
I have a class (RandomGenerator) that I created to be a random generator (obvious). This class have one method:
RandomGenerator.h****************
#import <Cocoa/Cocoa.h>
@interface RandomGenerator : NSObject
{
int range;
}
- (int) randomNumber: (int) withRange;
RandomGenerator.m****************
@implementation RandomGenerator
- (int) randomNumber: (int) withRange
{
if (range<1)
{
return 0;
} else
{
int random_number=(random() % range)+1;
return random_number;
}
}
@end
You have a method parameter, withRange, but you're not using it. Instead, you're using the instance variable of the class, range, which as far as your example shows, you're not setting to anything. If you don't set the instance variable of an Objective-C object to anything, it will default to 0. (This is unlike C++, where similar object member data values are undefined.) You should probably replace uses of "range" above with "withRange", and if you're not using the range instance variable for anything else, you can delete it from the class declaration.
I have another class (Controller) to control the rest of the program. I tried to use the method randomNumber in this class, but doesn't work.
Controller.h****************
#import <Cocoa/Cocoa.h>
@interface Controller : NSObject
{
IBOutlet NSTextField *Text_field;
}
- (IBAction)showRandom:(id)sender;
@end
Controller.m****************
#import "Controller.h"
#import "RandomGenerator.h"
@implementation Controller
- (IBAction)showRandom:(id)sender
{
[Text_field setIntValue:[self randomNumber:115]];
}
@end
Note "self" in Objective-C is like "this" in C++, it means you're referring to the current object. The current object here is an instance of Controller, which inherits from NSObject. Since Controller doesn't inherit from RandomGenerator (which you probably don't mean for it to do), Controller doesn't implement the -randomNumber: method, which is what the compiler is complaining about.
If you wish to use an object of type RandomGenerator here, you could do the following:
RandomNumber *randomNumber = [[RandomNumber alloc] init];
[Text_field setIntValue:[randomNumber randomNumber:115]];
[randomNumber release];
Above, I'm creating an instance of RandomNumber, using it, and then releasing it.
You could also make the -randomNumber: method a class method, instance of an instance method, if it doesn't require any state. Change the - in front of the method signature to +, and then you can do the following:
[Text_field setIntValue:[RandomNumber randomNumber:115]];
Here, you don't have to create an object, because you're using a method directly on the class. This tends to be done with utility methods like this, where the functionality is useful to be grouped under a certain class name, but again, no state is necessary.
You may want to take a look at a document like the Cocoa Fundamentals Guide, available at this link:
-- Andrew