@alexsteadfast
I am noob

Как обойти ошибку Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value?

Код контроллера:
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: [[String: String]]]
       )
        
       self.view.addSubview(view)
        
       let user = UserModel(url: "")
       user.test()
        
    }

}


Код вьюхи:

import UIKit

class HomeView: UIView {

    init(structure: [String : [[String : 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")
    }
    
}


Скрин проблемы:
622078045b00f220542033.png
  • Вопрос задан
  • 265 просмотров
Решения вопроса 1
@alexsteadfast Автор вопроса
I am noob
Проблема решена, приведением всех значений к строке
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы