I'm currently making an app similar to Morse Code, to encode, say A = hello, B = good by using isEqualToString
method.
-(NSString*)checkWords :(NSString*)words{
NSString * letter;
if (([words isEqualToString:@"a"]) || ([words isEqualToString:@"A"])){
letter = @"hello"; }
if (([words isEqualToString:@"b"]) || ([words isEqualToString:@"B"])){
letter = @"good"; }
return letter;
}
By clicking the button below will generate the code:
- (IBAction)decodeBtn:(id)sender {
outputTextField.text = @"";
NSString * inputString = outputView.text;
int wordLength = [inputString length]; //gets a count of length
int i = 0;
while (i < wordLength) {
unichar charToCheck = [inputString characterAtIndex:i];
if (charToCheck != 32){ // checks to make sure its not a space
NSString* words = [NSString stringWithCharacters:&charToCheck length:1];
NSString * letter = [self checkWords:words];
NSString * stringToAppend = outputTextField.text;
if (letter != @""){
outputTextField.text = [stringToAppend stringByAppendingString:letter];
} else {
// new line?
}
letter = nil;
}
i++;
}
}
I can get the alphabets to those words I needed. I wonder which method I should use to decode the words back to the alphabets? That is, when user input "hello good" and the output would be "A B"?
Thanks a lot.
If I write in this way, the app crashes:
[EncodeViewController copyWithZone:]: unrecognized selector sent to instance
-(NSString*)checkWords :(NSString*)words{
NSString * letter;
if ([words isEqualToString:@"hello"]) letter = @"A";
if ([words isEqualToString:@"good"]) letter = @"B";
return letter;
}
NSArray* words = [sentence componentsSeparatedByString:@" '];
NSMutableString* output = [NSMutableString string];
for (NSString* word in words) {
word = [word lowercaseString];
NSString* letter = [translationDict objectForKey:word];
[output appendFormat:@"%@ ", letter];
}
To create your translationDict use:
NSDictionary* translationDict = [NSDictionary dictionaryWithObjectsAndKeys:@"apple", @"A", @"banana", @"B", @"chocho", @"C", @"dingodog", @"D", .... @"Z", nil];
You can then use the translation loop either direction (if your individual letters are separated by blanks), with the order of the keys and values reveresed in translationDict
.