【问题标题】:about Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value [duplicate]关于线程 1:致命错误:在隐式展开可选值时意外发现 nil [重复]
【发布时间】:2020-02-03 14:33:12
【问题描述】:

我有一个线程 1:致命错误:在我的项目中隐式展开可选值错误时意外发现 nil 我托盘很多,但我无法修复它。任何人都可以帮助我。 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()
           }

    }

}

有人帮忙吗?

【问题讨论】:

  • UserImage 插座未连接。有关详细信息,请参阅副本。请务必将代码发布为文本,而不是图片。
  • 对此我深表歉意,但我确保他真的有联系
  • 如果您的问题中没有更多信息,则无法提供任何进一步的帮助。
  • 我从数据库中检索了一个图像,我将其解码以将其显示在 (UserImage) 中,也在同一个视图中,我有用于绘图的类(画布)

标签: swift xcode image error-handling


【解决方案1】:

DecodedImage 为零。你应该检查一下,为什么它是 nil。

最好询问一个可选项是否为 nil,例如

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

【讨论】:

  • decodedimage 为 nil 不会导致任何错误。
  • 我试过了,但是不行:(
猜你喜欢
  • 1970-01-01
  • 2020-09-03
  • 1970-01-01
  • 2022-07-07
  • 2021-01-05
  • 1970-01-01
  • 1970-01-01
  • 2020-11-01
  • 1970-01-01
相关资源
最近更新 更多