Reputation: 192
I'm currently trying to learn Objective C and by this way, Oriented Object languages.
I'm declaring variables from a class I've written, but, my functions are way too long, and I'd like to cut that code off.
I do not know how the return works with classes and that's my problem.
Grapejuice *juice;
juice = [[Grapejuice alloc] init];
[juice setName:@"Grape juice"];
[juice setOrderNumber:1000];
[juice setPrice:1.79];
This, is part of a main in which I'm doing this to several objects, how can I do that in a separated function, and still got these informations out of this new function to be re-used later (to be printed for example) ? Not sure if I'm clear but I've just started learning it yesterday, still hesitating on the basics.
Thanks homies.
Upvotes: 0
Views: 35
Reputation: 318814
If I understand you correctly, I believe what you want is a custom "init" method for your Grapejuice
class.
In Grapejuice.h, add:
- (instancetype)initWithName:(NSString *)name orderNumber:(NSInteger)number price:(double)price;
In Grapejuice.m, add:
- (instancetype)initWithName:(NSString *)name orderNumber:(NSInteger)number price:(double)price {
self = [super init];
if (self) {
_name = name;
_orderNumber = number;
_price = price;
}
return self;
}
Then to use that code you do:
Grapejuice *juice = [[Grapejuice alloc] initWithName:@"Grape Juice" orderNumber:1000 price:1.79];
Please note that you may need to adjust the data types for the orderNumber
and price
parameters. I'm just guessing. Adjust them appropriately based on whatever type you specified for the corresponding properties you have on your Grapejuice
class.
Upvotes: 2