I'm writing a class called CalcyrNumber. Each instance contains a scaler value, an array of warnings (IE, if the user attempted dividing by zero or passed in a non-number), and a few other things that aren't important for the snippet of code I'm asking about.
The class can be initialized from a string. If the string contains any operations, it should perform those operations. These are done by splitting the string in half at the operator, recursively parsing each half with the same initWithString function, and then combining them.
Parsing is done with another class called CalcyrBase, which looks perfectly fine to me. It's able to handle user defined bases, which is why I'm using it rather than the number parses Apple provides.
What concerns me is how I'm recursively calling initWithString: is this a proper thing to do in Obj-C, or is there a better solution that I'm missing? I've bolded the line that concerns me for emphasis.
The class can be initialized from a string. If the string contains any operations, it should perform those operations. These are done by splitting the string in half at the operator, recursively parsing each half with the same initWithString function, and then combining them.
Parsing is done with another class called CalcyrBase, which looks perfectly fine to me. It's able to handle user defined bases, which is why I'm using it rather than the number parses Apple provides.
What concerns me is how I'm recursively calling initWithString: is this a proper thing to do in Obj-C, or is there a better solution that I'm missing? I've bolded the line that concerns me for emphasis.
Code:
- (CalcyrNumber *)initWithString:(NSString*)string {
if (self = [super init]) {
// detect and handle operations here.
NSUInteger loc = [string rangeOfString:@"+"].location;
if (loc != NSNotFound) {
[b]self = [self initWithString:[string trimmedSubstringToIndex:loc]];[/b]
[self assignmentAdd:[[CalcyrNumber alloc] initWithString:[string trimmedSubstringFromIndex:loc + 1]]];
return self;
}
self.warnings = [[NSMutableArray alloc] init];
self.scaler = [CalcyrBase parseNumber:string warnings:self.warnings];
}
return self;
}
Last edited: