0

I’m trying to make Xcode print "Nice!" when you type in "Hi". I've used a IBOutlet, but I don’t know how to use the user input in my code. Also BTW I'm using Storyboard and not SwiftUI. It also gives me an error when I try to compare the datatype UIViewController and a String. Here is my view controller code(with the default App Delegate and Scene Delegate code):

import UIKit

class ViewController: UIViewController {

 @IBOutlet var yeet: [UITextField]!
 override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
 }

 func fuel(_ yeet:UIViewController) -> Int {

    if yeet == ("hi") {
        print("Nice!")
    }

 }
}
2
  • 1
    the error is straight forward. the yeet is of type UIViewController and you are comparing it with "hi" which is a string. Commented Dec 6, 2019 at 20:55
  • 1
    is there a reason why you declared the outlet as an array of text fields? Commented Dec 6, 2019 at 20:56

1 Answer 1

2

your textfield show be setup as

@IBOutlet weak var textFeildName: UITextField!

you will need to change a couple things inside of your file to prevent a crash. I'd delete the textfield and drag it into the assistant view and give it a new name.

but before you press "connect" press the "outlet" tab and change it to "Action" and then a new selector should come up select "Editing Did End" and go to the top and press "Did End On Exit"

after that is done would want to reference the variable of the text field:

example:

    @IBAction func TextFieldName(_ sender: Any) {



         if(self.TextFeildName.text!.contains("hi")){

                print("Nice!")

             }

    }

On top of all this, you do not compare strings with == that's only if you compare 2 separate strings for example stringOne == stringTwo if you are comparing or asking if a string contains anything you'd want to use the developing language specific string container IE: .contains

Also, please do not include "Xcode" as a tag with your question, as that should be reserved for Xcode related problems. not Swift or objective-c coding issues.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.