Sorry for this newbie question.
I wrote a class method that takes three lines and returns a substring.
I wrote conditional statements that allow a substring to be returned only if certain criteria are met. However, I am not sure what I need to return if the substring cannot be extracted. At the moment, I have a method that returns the string "error" by default, but I have the feeling that this may not be the best practice.
Here is my method:
+(NSString *)ExtractSubstringFrom:(NSString *)sourceString
Between:(NSString *)firstString And:(NSString *)secondString {
NSRange stringRangeOne = [sourceString rangeOfString:secondString];
NSString *resultString;
if (stringRangeOne.location != NSNotFound) {
resultString = [sourceString substringToIndex:stringRangeOne.location];
}
NSRange stringRangeTwo = [sourceString rangeOfString:firstString];
if (stringRangeTwo.location !=NSNotFound) {
resultString = [resultString substringFromIndex:stringRangeTwo.location+stringRangeTwo.length];
return resultString;
}
else return @"Error!";
}
How can I make this method more error-friendly?
source
share