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