Sha256: fa03c49980d75f668fdf15ced9ae04fd4d860b15c4dd8ebefb2655b9dfc2c012

Contents?: true

Size: 1.31 KB

Versions: 396

Compression:

Stored size: 1.31 KB

Contents

#import <Foundation/Foundation.h>
#import "WordCountExample.h"

@interface WordCount ()

@property (strong,nonatomic) NSArray *words;

@end

@implementation WordCount

- (id)initWithString:(NSString *)string {
    
    self = [super init];
    
    if (self) {
        NSString *cleanString = [self stripUnwantedCharacters:[string lowercaseString]];
        [self setWords:[cleanString componentsSeparatedByString:@" "]];
    }
    
    return self;
}

- (NSDictionary *)count {
    
    NSMutableDictionary *countDictionary = [NSMutableDictionary dictionary];
    
    [[self words] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        NSNumber *count = [countDictionary objectForKey:obj];
        
        if (count == NULL) { count = @0; }
        
        if (![(NSString *) obj isEqual: @""] ) {
            
            countDictionary[obj] = [NSNumber numberWithInt: count.intValue + 1];
        }
    }];
    
    return countDictionary;
}

- (NSString *)stripUnwantedCharacters:(NSString *)text {
    NSError *error = NULL;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[^\\w\\s]+" options:NSRegularExpressionCaseInsensitive error:&error];
    
    return [regex stringByReplacingMatchesInString:text options:0 range:NSMakeRange(0, [text length]) withTemplate:@""];
}

@end

Version data entries

396 entries across 396 versions & 1 rubygems

Version Path
trackler-2.2.1.98 tracks/objective-c/exercises/word-count/WordCountExample.m
trackler-2.2.1.97 tracks/objective-c/exercises/word-count/WordCountExample.m
trackler-2.2.1.96 tracks/objective-c/exercises/word-count/WordCountExample.m
trackler-2.2.1.95 tracks/objective-c/exercises/word-count/WordCountExample.m
trackler-2.2.1.94 tracks/objective-c/exercises/word-count/WordCountExample.m
trackler-2.2.1.93 tracks/objective-c/exercises/word-count/WordCountExample.m
trackler-2.2.1.92 tracks/objective-c/exercises/word-count/WordCountExample.m
trackler-2.2.1.91 tracks/objective-c/exercises/word-count/WordCountExample.m
trackler-2.2.1.90 tracks/objective-c/exercises/word-count/WordCountExample.m
trackler-2.2.1.89 tracks/objective-c/exercises/word-count/WordCountExample.m
trackler-2.2.1.88 tracks/objective-c/exercises/word-count/WordCountExample.m
trackler-2.2.1.87 tracks/objective-c/exercises/word-count/WordCountExample.m
trackler-2.2.1.86 tracks/objective-c/exercises/word-count/WordCountExample.m
trackler-2.2.1.85 tracks/objective-c/exercises/word-count/WordCountExample.m
trackler-2.2.1.84 tracks/objective-c/exercises/word-count/WordCountExample.m
trackler-2.2.1.83 tracks/objective-c/exercises/word-count/WordCountExample.m
trackler-2.2.1.82 tracks/objective-c/exercises/word-count/WordCountExample.m
trackler-2.2.1.81 tracks/objective-c/exercises/word-count/WordCountExample.m
trackler-2.2.1.80 tracks/objective-c/exercises/word-count/WordCountExample.m
trackler-2.2.1.79 tracks/objective-c/exercises/word-count/WordCountExample.m