Editor placeholder in source file

Hey, I'm new to coding and trying to deep dive into Xcode and swift by creating an app of my own.

I've got so far but can't work out how to resolve the following error in my code. 'editor placeholder in source file'

Part of code in question..

 func textFieldShouldReturn(_ textField: UITextField) -> Bool {

            textField.resignFirstResponder()

            let newText = textField.text

            if let index = textFields.firstIndex(of: textField) {

                textFieldStrings[index] = newText // this bit!! //

               UserDefaults.standard.set(newText, forKey: textFieldKeys[index])

            }

            return true

        }

newText is an Optional String, so you need to unwrap it, or provide a default value. Maybe...

textFieldStrings[index] = newText ?? "some default value"

Other than that, you are using several variables where you don't show the declarations.
So there could be a problem with one of those?

When you post code, don't post in comments. It is totally messed up.

Here it is formatted:

class ViewController: UIViewController, UITextFieldDelegate {
    @IBOutlet weak var benchPressPB: UITextField!
    @IBOutlet weak var squatPB: UITextField!
    @IBOutlet weak var deadliftPB: UITextField!
    @IBOutlet weak var ohpPB: UITextField!
    @IBOutlet weak var rackPullPB: UITextField!
    @IBOutlet weak var legPressPB: UITextField!
    @IBOutlet weak var pullUpsPB: UITextField!
    
    var textFields =     var textFieldKeys = [       "benchPressPB",       "squatPB",       "deadliftPB",       "ohpPB",       "rackPullPB",       "legPressPB",       "pullUpsPB"     ]
    var textFieldStrings =
        
        override func viewDidLoad() {
            super.viewDidLoad()
            self.benchPressPB.delegate = self
            self.squatPB.delegate = self
            self.deadliftPB.delegate = self
            self.ohpPB.delegate = self
            self.rackPullPB.delegate = self
            self.legPressPB.delegate = self
            self.pullUpsPB.delegate = self
            textFields = [benchPressPB, squatPB, deadliftPB, ohpPB, rackPullPB, legPressPB, pullUpsPB]
            for (index, key) in textFieldKeys.enumerated() {
                let aValue = UserDefaults.standard.string(forKey: key)
                textFields[index].text = aValue
                textFieldStrings.append(aValue ?? "")
                
            }
        }
    
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        let newText = textField.text
        if let index = textFields.firstIndex(of: textField) {
            textFieldStrings[index] = newText
            UserDefaults.standard.set(newText, forKey: textFieldKeys[index])
            
        } 
      return true
    }
}
// My full code for my app, to provide a new default value, do I first need to declare it as a variable? or am I completely missing the point. Apologies if this is a completely dumb question. I am literally brand new to this and was advised the best way to learn was to just build something instead of a boring udemy course. I had to delete some of the code to post... UITextFields() & UIString() Really appreciate all your help! 

There is something that doesn't mean anything:

 var textFields =   var textFieldKeys = [    "benchPressPB",    "squatPB",    "deadliftPB",    "ohpPB",    "rackPullPB",    "legPressPB",    "pullUpsPB"   ]   
var textFieldStrings =

The error here:

        if let index = textFields.firstIndex(of: textField) {
            textFieldStrings[index] = newText
            UserDefaults.standard.set(newText, forKey: textFieldKeys[index])
            
        } 

textField is UITextField ; textFields is an array of String (I assume) So you should search for newText. But it is an optional, you need to unwrap first

        if let newText = textField.text, let index = textFields.firstIndex(of: newText) {

Hey, thanks for the help! I've been trying to work this one out for a few days still. So do I need to delete the error here part and replace with the unwrapped line of code? I'm a little confused still.

I tried declaring the newText as a variable it got rid of the editor placeholder in source file error but I was met with plenty more. Sorry if this is very basic. Appreciate the help!

Accepted Answer

First point is to solve this:

 var textFields =   var textFieldKeys = [    "benchPressPB",    "squatPB",    "deadliftPB",    "ohpPB",    "rackPullPB",    "legPressPB",    "pullUpsPB"   ]   
var textFieldStrings =

What is your intent ? Where did you copy it from (with errors) ?

Do you mean :

var textFields :  [UITextField] = []
var textFieldKeys = [ "benchPressPB",    "squatPB",    "deadliftPB",    "ohpPB",    "rackPullPB",    "legPressPB",    "pullUpsPB" ] 
var textFieldStrings : [String] = []

If so, your code is now:

class ViewController: UIViewController, UITextFieldDelegate {
    @IBOutlet weak var benchPressPB: UITextField!
    @IBOutlet weak var squatPB: UITextField!
    @IBOutlet weak var deadliftPB: UITextField!
    @IBOutlet weak var ohpPB: UITextField!
    @IBOutlet weak var rackPullPB: UITextField!
    @IBOutlet weak var legPressPB: UITextField!
    @IBOutlet weak var pullUpsPB: UITextField!
    
    var textFields :  [UITextField] = []
    var textFieldKeys = ["benchPressPB", "squatPB", "deadliftPB", "ohpPB", "rackPullPB", "legPressPB", "pullUpsPB"]
    var textFieldStrings : [String] = []
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.benchPressPB.delegate = self
        self.squatPB.delegate = self
        self.deadliftPB.delegate = self
        self.ohpPB.delegate = self
        self.rackPullPB.delegate = self
        self.legPressPB.delegate = self
        self.pullUpsPB.delegate = self
        textFields = [benchPressPB, squatPB, deadliftPB, ohpPB, rackPullPB, legPressPB, pullUpsPB]
        for (index, key) in textFieldKeys.enumerated() {
            let aValue = UserDefaults.standard.string(forKey: key)
            textFields[index].text = aValue
            textFieldStrings.append(aValue ?? "")
            
        }
    }
    
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        let newText = textField.text ?? ""      // textField.text is an optional, but textFieldStrings[index] is String, not String? ; so ?? make it sure you get a String (maybe empty) and not String?
        if let index = textFields.firstIndex(of: textField) {
            textFieldStrings[index] = newText
            UserDefaults.standard.set(newText, forKey: textFieldKeys[index])
        }
        return true
    }
}

Try with those changes and tell what you get.

Thanks Claude, you've been a great help. I can sorta see how this works now with my very limited knowledge. I did have code after var TextFieldsStrings = (I think [string() though but it wouldn't let me send the code in here. (domain error)

May I ask how you learnt swift? and if you have any advice?

You probably had written

var TextFieldsStrings = [String] ()    // without the space

and got the error:

Please fix the following URL related errors. This domain "" is not a permitted domain on this forums.

This is a forum bug ! I filed a bug report on it.

So, does your code work now (or at least compile) ? If so, you should close the thread (just mark my previous answer as correct answer).

As for learning Swift, I think I did like a lot of us:

  • learn Xcode and iOS API (I used books that are now totally outdated)
  • work from books to practice Swift (Apple Books are pretty good)
  • used on line materials (Stanford course for iOS notably).
  • and then start developing some app, making extensive use of this forum and other information on the web each time I had a problem.
  • and most important, Kept continuing learning over the years…

Wish you good learning and enjoy.

Thanks Claude - that's exactly what happened.

yes my code complies now but I have a new set of problems, which I'm going to try tackle myself over the next week. I will take your advice on how to manage and learn how to approach these.

Of course, I'll mark the answer correct now and you too, have a nice day! Appreciate your help ALOT!

Editor placeholder in source file
 
 
Q