Swift 快速字处理
我正在做一个简单的swift应用程序。现在我有一个输入文本字段,它接受用户输入,并在单击按钮时将其输出到输出用户字段。如下代码所示:Swift 快速字处理,swift,Swift,我正在做一个简单的swift应用程序。现在我有一个输入文本字段,它接受用户输入,并在单击按钮时将其输出到输出用户字段。如下代码所示: import UIKit import Foundation class ViewController: UIViewController { @IBOutlet weak var userNameField: UITextField! @IBOutlet weak var textView: UITextView! overrid
import UIKit
import Foundation
class ViewController: UIViewController {
@IBOutlet weak var userNameField: UITextField!
@IBOutlet weak var textView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
userNameField.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@IBAction func enterTapped(_ sender: Any) {
textView.text = "\(userNameField.text!)"
}
}
extension ViewController : UITextFieldDelegate {
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
}
我想在文本显示给用户之前进行一些文字处理,但每次我尝试编码时都会出现一系列错误。我的文字处理代码应该放在代码中的什么位置。当我提到文字处理时,我指的是一些字符串命令,比如:split,combine…应该在这里
@IBAction func enterTapped(_ sender: Any) {
let str = userNameField.text!
// do what you want with str
let addComma = str + ","
textView.text = addComma
}
我的文字处理代码应该放在代码中的什么位置
您需要进行一些处理,并将处理后的文本放入out字段。因为这个过程需要在按钮动作上进行。我理解应按照以下方法进行处理:
@IBAction func enterTapped(_ sender: Any) {
// this is the correct place for processing your string.
let inputStr = userNameField.text!
//Process 'inputStr" as needed.
textView.text = inputStr
}
你会犯什么错误?