ObjC: Error handling ... what to return

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!";

             //To do... improve error checking

             }

How can I make this method more error-friendly?

+5
source share
2 answers

There are several ways to handle this type of error in Objective-C:

  • - nil. nil Objective C, .
  • NSError - , , , . API .
  • - " ", .. .
  • - , Apple . , .
+6

, " " , . , / , . (, ) , - .

Objective-C . Objective-C:

0

All Articles