Objective-C-使用字典将字符串翻译成不同的字符

梦想家

我有一个iOS程序,该程序从用户处获取一个字符串,然后按字符将字符串拆分,然后使用每个字符作为键来从字典中获取一个单独的(摩尔斯电码)字符。我遇到有关char * s和字符串的问题,并且收到与错误类型有关的错误,并且未在数组中找到键。有什么想法吗?提前致谢。

这是代码:

//
//  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
Matteo Gobbi

您的代码应使用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);
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章