Question

I have an iOS program that takes a string from the user, splits the string up by character, and then uses each character as a key to grab a seperate (morse code) character from a dictionary. I'm having issues regarding the char*s and strings, and I am receiving errors having to do with improper types and not finding the key in the array. Thoughts please? Thanks in advance.

Here is the code:

//
//  ViewController.m
//  MorseCodeTranslator
//
//  Created by Mitch  on 4/30/14.
//  Copyright (c) 2014 Mitch . All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *userInput;
@property (weak, nonatomic) IBOutlet UILabel *outputField;

- (IBAction)translateUserString:(UIButton *)sender;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (IBAction)translateUserString:(UIButton *)sender {
    BOOL stringPresent = (self.userInput.text.length > 0);

    if (stringPresent) {
        NSDictionary *morseCharacterKey = @{
                                           @"A" : @".-",
                                           @"B" : @"-...",
                                           @"C" : @"-.-.",
                                           @"D" : @"-..",
                                           @"E" : @".",
                                           @"F" : @"..-.",
                                           @"G" : @"--.",
                                           @"H" : @"....",
                                           @"I" : @"..",
                                           @"J" : @".---",
                                           @"K" : @"-.-",
                                           @"L" : @".-..",
                                           @"M" : @"--",
                                           @"N" : @"-.",
                                           @"O" : @"---",
                                           @"P" : @".--.",
                                           @"Q" : @"--.-",
                                           @"R" : @".-.",
                                           @"S" : @"...",
                                           @"T" : @"-",
                                           @"U" : @"..-",
                                           @"V" : @"...-",
                                           @"W" : @".--",
                                           @"X" : @"-..-",
                                           @"Y" : @"-.--",
                                           @"Z" : @"--..",
                                           @"1" : @".----",
                                           @"2" : @"..---",
                                           @"3" : @"...--",
                                           @"4" : @"....-",
                                           @"5" : @".....",
                                           @"6" : @"-....",
                                           @"7" : @"--...",
                                           @"8" : @"---..",
                                           @"9" : @"----.",
                                           @"0" : @"-----",
                                           @" " : @"/",
                                           @"," : @"--..--",
                                           @"." : @".-.-.-",
                                           @"?" : @"..--..",
                                           @"\'" : @".----.",
                                           @"!" : @"-.-.--",
                                           @"/" : @"-..-.",
                                           @"(" : @"-.--.",
                                           @")" : @"-.--.-",
                                           @"&" : @".-...",
                                           @":" : @"---...",
                                           @";" : @"-.-.-.",
                                           @"=" : @"-...-",
                                           @"+" : @".-.-.",
                                           @"-" : @"-....-",
                                           @"_" : @"..--.-",
                                           @"\"" : @".-..-.",
                                           @"$" : @"...-..-",
                                           @"@" : @".--.-."
        };

        NSString *userString = self.userInput.text;
        NSString *outputString;

        for (int i = 0; userString.length > i; i++){
            char userCharacter = [userString characterAtIndex:i];

            char morseCharacter = [morseCharacterKey objectForKey:morseCharacterKey[userCharacter]];
            outputString stringByAppendingString:[morseCharacter];

            self.outputField.text = outputString;
        }

    }
}
@end
Was it helpful?

Solution

Your code should use NSString:

for (int i = 0; userString.length > i; i++) {
        NSString *userCharacter = [userString substringWithRange:NSMakeRange(i, 1)];

        NSString *morseCharacter = morseCharacterKey[userCharacter];
        outputString = [outputString stringByAppendingString:morseCharacter];

        NSLog(outputString);
    }
}

OTHER TIPS

So:

@"A"

Is an NSString literal. So your dictionary keys:

NSDictionary *morseCharacterKey = @{
                                   @"A" : @".-",
                                   ...

Are NSString*s. Which is fine, because keys are required to be objects.

You're getting the character as a char, which is not an object, and even if it was it isn't the same object as you used for your keys.

Instead what you want to do is something like this:

NSRange range;
range.location = i;
range.length = 1;
NSString* userCharacter = [userString substringWithRange:range];

And use userCharacter as your key. Note that your values are also NSString*s, so you should store the result of your dictionary lookup in an NSString as well.

You shouldn't be working with char. Try this:

    NSString *userString = self.userInput.text;
    NSMutableString *outputString = [NSMutableString string];

    for (NSUInteger i = 0; i < userString.length; i++) {
        NSString *letter = [userString substringWithRange:NSMakeRange(i, 1)];

        NSString *morse = morseCharacterKey[letter];
        [outputString appendingString:morse];
    }

    self.outputField.text = outputString;

Block based enumeration would clean this up a little bit. Also a NSMutableString would be better then using stringByAppendingString. Also since I use a mutableString we don't have to use the __block specify in order to append to it like we would if stringByAppending string was used.

NSString *string = @"...";
NSMutableString *output = [[NSMutableString alloc] init];
[string enumerateSubstringsInRange:NSMakeRange(0, string.length) options:NSStringEnumerationByComposedCharacterSequences usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
    NSString *value = morseCharacterKey[substring];
    [output appendString:value];
}];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top