swift: 如何将一个字符串从textField 分配给另一个字符串?
swift: how to assign a string from textField to another string?
swift: 如何将一个字符串从 textField 分配给另一个字符串?
例如:
如果用户在 texField 中写入 "abc" 我希望 program/app 输出 "DoReMi"
a=Do
b=Re
c=Mi
谢谢
这样试试:
let inputString = "abc"
// create a dictionary with your notes
let notes = ["a":"Do","b":"Re","c":"Mi","d":"Fa","e":"So","f":"La","g":"Ti"]
// map the value of your string characters keys
let output = "".join( Array(inputString).map{ notes[ String([=10=]) ] ?? "" } )
println(output) // "DoReMi"
如果您希望它在用户输入时发生,您只需将 addTarget 添加到您的 textField 中,控件事件编辑更改如下:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var textField: UITextField!
@IBOutlet weak var label: UILabel!
let notes = ["a":"Do","b":"Re","c":"Mi","d":"Fa","e":"So","f":"La","g":"Ti"]
override func viewDidLoad() {
super.viewDidLoad()
textField.addTarget(self, action: "editingChanged:", forControlEvents: UIControlEvents.EditingChanged)
textField.becomeFirstResponder()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func editingChanged(sender: UITextField) {
sender.text = sender.text!.notesOnly
label.text = String(Array(textField.text!.characters).map{ self.notes[ String([=11=]) ] ?? "" })
}
}
extension String {
var notesOnly: String {
return componentsSeparatedByCharactersInSet(NSCharacterSet(charactersInString: "abcdefg").invertedSet).joinWithSeparator("")
}
}
希望我能理解您的问题:您希望能够接收一个字符串,然后根据用户输入的内容输出一个字符串,这样正确吗?您可以使用字典来完成此操作。在视图控制器文件的顶部添加一个变量,如下所示:
var myDic = ["a":"Do", "b":"Re", "c":"Mi"]
然后您所要做的就是检查文本字段字符串中的每个字符,它可能看起来像这样:
let stringArr = Array(textField.text)
var outStr = ""
for char in stringArr {
if let c = myDic[String(char)] {
outStr = outStr + c
}
}
println(outStr)
swift: 如何将一个字符串从 textField 分配给另一个字符串?
例如:
如果用户在 texField 中写入 "abc" 我希望 program/app 输出 "DoReMi"
a=Do
b=Re
c=Mi
谢谢
这样试试:
let inputString = "abc"
// create a dictionary with your notes
let notes = ["a":"Do","b":"Re","c":"Mi","d":"Fa","e":"So","f":"La","g":"Ti"]
// map the value of your string characters keys
let output = "".join( Array(inputString).map{ notes[ String([=10=]) ] ?? "" } )
println(output) // "DoReMi"
如果您希望它在用户输入时发生,您只需将 addTarget 添加到您的 textField 中,控件事件编辑更改如下:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var textField: UITextField!
@IBOutlet weak var label: UILabel!
let notes = ["a":"Do","b":"Re","c":"Mi","d":"Fa","e":"So","f":"La","g":"Ti"]
override func viewDidLoad() {
super.viewDidLoad()
textField.addTarget(self, action: "editingChanged:", forControlEvents: UIControlEvents.EditingChanged)
textField.becomeFirstResponder()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func editingChanged(sender: UITextField) {
sender.text = sender.text!.notesOnly
label.text = String(Array(textField.text!.characters).map{ self.notes[ String([=11=]) ] ?? "" })
}
}
extension String {
var notesOnly: String {
return componentsSeparatedByCharactersInSet(NSCharacterSet(charactersInString: "abcdefg").invertedSet).joinWithSeparator("")
}
}
希望我能理解您的问题:您希望能够接收一个字符串,然后根据用户输入的内容输出一个字符串,这样正确吗?您可以使用字典来完成此操作。在视图控制器文件的顶部添加一个变量,如下所示:
var myDic = ["a":"Do", "b":"Re", "c":"Mi"]
然后您所要做的就是检查文本字段字符串中的每个字符,它可能看起来像这样:
let stringArr = Array(textField.text)
var outStr = ""
for char in stringArr {
if let c = myDic[String(char)] {
outStr = outStr + c
}
}
println(outStr)