Gian.Pit
Gian.Pit

Reputation: 1

Swift word processing

I'm making a simple swift application. Right now I have an input text field that takes a user input and outputs it to the output user field when a button is clicked. As shown in the code below:

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
    }    
}

I wanted to perform some word processing before the text is displayed to the user but, every time I try coding I get a bunch of errors. Where in the code should my word processing code go. When I refer to word processing I'm referring to some string commands such as: split, combine...

Upvotes: 0

Views: 190

Answers (2)

Rizwan
Rizwan

Reputation: 3666

Where in the code should my word processing code go.

you want to do some processing and put the processed text in the out-filed. As this process needs to take place on button action. I understand the processing should go in following method:

@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
}

Upvotes: 0

Shehata Gamal
Shehata Gamal

Reputation: 100503

It should be here

@IBAction func enterTapped(_ sender: Any) {

  let str = userNameField.text!

  // do what you want with str

  let addComma = str + ","

  textView.text = addComma
}

Upvotes: 1

Related Questions