私は素早くモールス変換コンバーターを試しています。私が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
に単純化することができますあなたの辞書は明示的な型です: 'let alphaNumToMorse:[String:String] = ['。大きな辞書リテラルのタイプが失敗するか、コンパイル時間の結果が悪くなる可能性が推測されます。 – vacawama