A
A
Alexander Ivanov2022-03-03 11:10:54
Swift
Alexander Ivanov, 2022-03-03 11:10:54

How to get around Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value?

Controller code:

import UIKit

class ViewController: UIViewController {
    
    func setLogin() {
        print("setLogin")
    }
    
    var titleField = [
        "Hello"
    ]
    
    var interestingNumbers = [
        "fields": [
            [
                "text": "xxxxx",
                "type": "UITextField",
                "x": 100,
                "y": 100,
                "width": 100,
                "height": 100
            ],
            [
                "text": "yyyy",
                "type": "UITextField",
                "x": 100,
                "y": 100,
                "width": 100,
                "height": 100
            ]
        ]
    ]
    
    

    
    override func viewDidLoad() {
        super.viewDidLoad()

       let view = HomeView(
           structure: interestingNumbers as! [String: ]
       )
        
       self.view.addSubview(view)
        
       let user = UserModel(url: "")
       user.test()
        
    }

}


View code:

import UIKit

class HomeView: UIView {

    init(structure: [String :  ]) {
        super.init(frame: .zero)
        
        print("structure")
        
        self.setupBackground()
        for key in structure["fields"]! {
            var field: UITextField
            switch key["type"]!{
                case "UITextField":
                    field = UITextField()
                    field.text = key["text"]
                    field.frame = CGRect(
                        x: Int(key["x"]!)!,
                        y: Int(key["y"]!)!,
                        width: Int(key["width"]!)!,
                        height: Int(key["height"]!)!
                    )
                    self.addSubview(field)
                break;
                default: break
            }
            
        }

    }
    
    
    
    func setupBackground(){
        self.frame = CGRect(x: 0, y: 0, width: 1000, height: 1000)
        self.backgroundColor = UIColor(patternImage: UIImage(named: "bg.jpeg")!)
        let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.dark)
        let blurEffectView = UIVisualEffectView(effect: blurEffect)
        blurEffectView.frame = self.bounds
        blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        blurEffectView.alpha = 0.8
        self.addSubview(blurEffectView)
    }
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    deinit {
        print("deinit HomeView")
    }
    
}


Problem screenshot:
622078045b00f220542033.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Ivanov, 2022-03-03
@alexsteadfast

Problem solved by casting all values ​​to a string

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question