摩尔斯电码转换器不能快速工作

时间:2017-03-27 14:53:06

标签: swift swift-playground

我正在尝试一个快速的莫尔斯代码转换器游乐场。当我尝试在iPad上运行它时,我告诉我检查我的代码时出错。没有构建时间错误,我有我需要的一切。有什么我忘记包括的吗?

import UIKit
import Foundation
import AVFoundation
import PlaygroundSupport


let alphaNumToMorse = [
    "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": "--..",
    "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": "-----",
    " ": " ",
]

// UI
let window = UIView(frame: CGRect(origin: CGPoint(x: 100, y: 100), size: CGSize(width: 650, height: 650)))
window.backgroundColor = UIColor.white

let titleLabel = UILabel(frame: CGRect(origin: CGPoint(x: 20, y: 50), size: CGSize(width: 500, height: 50)))
titleLabel.text = "Morse Code Translator"
titleLabel.font = UIFont.systemFont(ofSize: 50, weight: UIFontWeightThin)
window.addSubview(titleLabel)

let directionsLabel = UILabel(frame: CGRect(origin: CGPoint(x: 20, y: 100), size: CGSize(width: 300, height: 50)))
directionsLabel.text = "Type text to turn it into morse code."
window.addSubview(directionsLabel)

let inputField = UITextField(frame: CGRect(origin: CGPoint(x: 20, y: 150), size: CGSize(width: 610, height: 50)))
inputField.text = ""
inputField.placeholder = "Alpha-numeric characters only (ABC, abc, 123)."
inputField.borderStyle = UITextBorderStyle.roundedRect
window.addSubview(inputField)

let translatedLabel = UILabel(frame: CGRect(origin: CGPoint(x: 20, y: 500), size: CGSize(width: 610, height: 150)))
translatedLabel.font = UIFont.systemFont(ofSize: 12, weight: UIFontWeightThin)
translatedLabel.lineBreakMode = NSLineBreakMode.byWordWrapping
translatedLabel.numberOfLines = 10
window.addSubview(translatedLabel)

let outputLabel = UILabel(frame: CGRect(origin: CGPoint(x: 20, y: 700), size: CGSize(width: 610, height: 150)))
outputLabel.text = ""
window.addSubview(outputLabel)

// Conversion
func convertLetterToMorse(_ input: Character) -> String {
    var returnChar = alphaNumToMorse[String(input)]
    if returnChar == nil {
        returnChar = ""
    }
    return returnChar!
}

var stringToConvert = String()
func convertStringToMorse(_ input: String) -> String {
    stringToConvert = input
    let charsInString = input.characters
    var returnString = ""
    for char in charsInString {
        let returnChar = convertLetterToMorse(char)
        if returnChar != "" {
            returnString += returnChar + " "
            outputLabel.text = "\(returnString)"
        }
    }
    return returnString
}

class convert: NSObject {
    func convert(sender: UIButton!) {
        let textFieldText = inputField.text
        if textFieldText != nil {
            let outputText = convertStringToMorse(textFieldText!)
            translatedLabel.text = "\(outputText)"
        }

    }
}

let responder = UIResponder()
let convertButton = UIButton(frame: CGRect(origin: CGPoint(x: 20, y: 200), size: CGSize(width: 100, height: 50)))
convertButton.setTitle("Convert!", for: .normal)
convertButton.setTitleColor(#colorLiteral(red: 0, green: 0.5008062124, blue: 1, alpha: 1), for: .normal)
convertButton.addTarget(responder, action: #selector(convert.convert), for: .touchUpInside)
window.addSubview(convertButton)

PlaygroundPage.current.liveView = window

2 个答案:

答案 0 :(得分:2)

您应该会看到类似

的错误消息
-[UIResponder convertWithSender:]: unrecognized selector sent to instance 0x610000018d20

按下按钮时。问题出在这里:

let responder = UIResponder()
// ...
convertButton.addTarget(responder, action: #selector(convert.convert), for: .touchUpInside)

操作消息将发送到UIResponder的实例,而不是convert类的实例。

let responder = convert()

应该解决这个问题。 (但请注意,Swift中的类型通常会启动 用大写字母。)

备注:转换功能可以简化为

func convertStringToMorse(_ input: String) -> String {
    return input.characters
        .flatMap { alphaNumToMorse[String($0)] }
        .joined(separator: " ")
}

答案 1 :(得分:0)

您在Convert类中使用inputField,但您从未定义它。

 let textFieldText = inputField.text

对于translateLabel也一样。