Skip to content

Instantly share code, notes, and snippets.

@chischaschos
Created April 1, 2014 03:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chischaschos/9907251 to your computer and use it in GitHub Desktop.
Save chischaschos/9907251 to your computer and use it in GitHub Desktop.
#import "Bob.h"
@implementation Bob
- (NSString *) hey: (NSString *) message {
if ([self isEmpty:message]) {
return @"Fine, be that way.";
} else if ([self isScreaming:message]) {
return @"Woah, chill out!";
} else if ([self isQuestion:message]) {
return @"Sure.";
} else {
return @"Whatever.";
}
}
- (BOOL) isQuestion: (NSString *) message {
return [message hasSuffix:@"?"];
}
-(BOOL) isEmpty: (NSString *) message {
NSString *trimmedMessage = [message stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
return [trimmedMessage length] == 0;
}
- (BOOL) isScreaming: (NSString *) message {
NSError *error = nil;
NSRegularExpression *regexp = [NSRegularExpression regularExpressionWithPattern:@"^[,.?\\s\\d]+$"
options:0
error:&error];
NSUInteger numberOfMatches = [regexp numberOfMatchesInString:message
options:0
range:NSMakeRange(0, [message length])];
return [message isEqualToString:[message uppercaseString]] && numberOfMatches == 0;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment