关于线程1:致命错误:意外地发现nil,同时隐式展开一个可选值

时间:2019-10-05 16:23:34

标签: swift xcode image error-handling

我有一个线程1:致命错误:出乎意料地发现了nil,同时在我的项目中隐式展开了一个Optional值错误,我托盘了很多,但我无法修复。任何人都可以帮助我。 enter image description here 这是代码: 我认为问题出在类(画布)上,因为当我调用视图时会出现问题,但是我的应用程序需要这样做。

    import UIKit
import Foundation
class WarmUpControlClass: UIViewController {

    @IBOutlet weak var UserImage: UIImageView!
    let canvas = WarmUp()
    //func to creates and instantiates the UIView object (warmUp class object)
   override func loadView() {
 //to call functions in the warm up class to enable user drawing

   self.view = canvas

    }
    override func viewDidLoad() {
         //to show the user image in the top right corner

              let decodedData = Data(base64Encoded: SelectedUser.ImageString, options: .ignoreUnknownCharacters)
              let decodedimage:UIImage? = UIImage(data: decodedData! as Data)
              UserImage.image = decodedimage //i put .image to convert from UIimage to UIimageVeiw
              UserImage.image = decodedimage
              UserImage.layer.cornerRadius = UserImage.frame.size.width/2
              UserImage.clipsToBounds = true


        canvas.backgroundColor = .white //to set white background to drawing screen
        super.viewDidLoad()
}//end conterol class
    //this class to do all drawing methods (user writing)
    class WarmUp: UIView {



        override func draw(_ rect: CGRect) {
               super.draw(rect)  //custom drawing
               guard let context = UIGraphicsGetCurrentContext() else
               { return }
            context.setStrokeColor(UIColor.gray.cgColor)
               context.setLineWidth(8)
               context.setLineCap(.round)
               lines.forEach { (line) in
                       for(i , p) in line.enumerated(){
                           if i == 0 {
                           context.move(to: p)
                           } else {
                           context.addLine(to: p)
                           }
                       }//end for
                    }
               context.strokePath()
           }
           var lines = [[CGPoint]]()//two dimantional array
           override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
               lines.append([CGPoint]())
           }
           //track the finger as we move across screen
           override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
               guard let point = touches.first?.location(in: nil) else {return}

               guard var lastLine = lines.popLast() else { return }
               lastLine.append(point)

               lines.append(lastLine)

               setNeedsDisplay()
           }

    }

}

有人帮忙吗?

1 个答案:

答案 0 :(得分:-2)

DecodedImage为零。您应该检查为什么它为零。

总是最好问一个可选的是nil例如。

If let image = imageOptional {
// in here image is not optional
} else {
// maybe print error here
}