Fatal error: Unexpectedly found nil while unwrapping an Optional value

@IBOutlet weak var surnameTextField: UITextField! surnameTextField.backgroundColor = UIColor.clear surnameTextField.tintColor = .white surnameTextField.textColor = .white surnameTextField.attributedPlaceholder = NSAttributedString(string: surnameTextField.placeholder!, attributes: [NSAttributedString.Key.foregroundColor: UIColor(white: 1.0, alpha: 0.6)]) let bottomLayersurname = CALayer() bottomLayersurname.frame = CGRect(x: 0, y: 29, width:1000, height: 0.6) bottomLayersurname.backgroundColor = UIColor.lightGray.cgColor surnameTextField.layer.addSublayer(bottomLayersurname)

Answered by DTS Engineer in 826136022

In the documentation it says that the placeholder string is nil by default: https://developer.apple.com/documentation/uikit/uitextfield/placeholder

You can use the Inspector in Xcode to set the value to a string, or you can add a test into your code as follows:

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var surnameTextField: UITextField!
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        surnameTextField.backgroundColor = UIColor.clear
        surnameTextField.tintColor = .white
        surnameTextField.textColor = .white
        surnameTextField.attributedPlaceholder =
        NSAttributedString(string:
                            (surnameTextField.placeholder != nil ? surnameTextField.placeholder! : ""),
                           attributes: [NSAttributedString.Key.foregroundColor: UIColor(white: 1.0, alpha: 0.6)])

        let bottomLayersurname = CALayer()
        bottomLayersurname.frame = CGRect(x: 0, y: 29, width:1000, height: 0.6)
        bottomLayersurname.backgroundColor = UIColor.lightGray.cgColor
        
        surnameTextField.layer.addSublayer(bottomLayersurname)
    }
}

In the documentation it says that the placeholder string is nil by default: https://developer.apple.com/documentation/uikit/uitextfield/placeholder

You can use the Inspector in Xcode to set the value to a string, or you can add a test into your code as follows:

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var surnameTextField: UITextField!
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        surnameTextField.backgroundColor = UIColor.clear
        surnameTextField.tintColor = .white
        surnameTextField.textColor = .white
        surnameTextField.attributedPlaceholder =
        NSAttributedString(string:
                            (surnameTextField.placeholder != nil ? surnameTextField.placeholder! : ""),
                           attributes: [NSAttributedString.Key.foregroundColor: UIColor(white: 1.0, alpha: 0.6)])

        let bottomLayersurname = CALayer()
        bottomLayersurname.frame = CGRect(x: 0, y: 29, width:1000, height: 0.6)
        bottomLayersurname.backgroundColor = UIColor.lightGray.cgColor
        
        surnameTextField.layer.addSublayer(bottomLayersurname)
    }
}
Fatal error: Unexpectedly found nil while unwrapping an Optional value
 
 
Q