I’m so stuck with my beginner coding project. Editor placeholder in source file error need to unwrap optional string
My app is a fitness tracker just to track PB’s but I can’t get it, to store the values inputted once the app closes. I have created the array of the options but I can’t seem to figure out how to unwrap the code for the string "newText"
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
}
}
2
Answers
You can also unwrap it in the
if let ... = ...
. To find out why theif
condition is separated by a comma, see this post.Code:
The "placeholder in source code" error is from Xcode’s auto-fill.
As you type a method name, Xcode fills out templates for the various classes and functions you enter. Those templates include "placeholders" for the various parameters. The editor displays them as colored text, and when you tap on a placeholder, it lets you replace it with actual code.
An example of a function template with placeholders:
The
<#T##TimeInterval#>
bit is a placeholder for the string "TimeInterval"It looks like this in the Xcode editor: