gpt4 book ai didi

ios - 困惑 : Non-nil value unwrapped nil, if-let 子句在 nil 值上执行 block

转载 作者:行者123 更新时间:2023-11-28 05:55:08 26 4
gpt4 key购买 nike

我将数据类型的非零值分配给非可选属性,然后将其分配给可选属性,最后用所述数据实例化图像。当可选项通过 if-let 子句时,它的 block 执行,抛出错误:

Fatal error: Unexpectedly found nil while unwrapping an Optional value

绝不应该 stillImageData 解包 nil。

import UIKit
import AVFoundation
import Photos

class ViewController: UIViewController, AVCapturePhotoCaptureDelegate {

var photoOutput: AVCapturePhotoOutput!
var stillImageData: Data!

@IBAction func handleTouch(_ sender: Any) {
let photoSettings = AVCapturePhotoSettings(format: nil)
photoSettings.isAutoStillImageStabilizationEnabled = true
if photoOutput.supportedFlashModes.contains(AVCaptureDevice.FlashMode.auto) {
photoSettings.flashMode = .auto
}
photoOutput.capturePhoto(with: photoSettings, delegate: self)
}

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

switch AVCaptureDevice.authorizationStatus(for: .video) {
case .authorized: // The user has previously granted access to the camera.
self.setupCaptureSession()

case .notDetermined: // The user has not yet been asked for camera access.
AVCaptureDevice.requestAccess(for: .video) { granted in
if granted {
self.setupCaptureSession()
}
}

case .denied: // The user has previously denied access.
return
case .restricted: // The user can't grant access due to restrictions.
return
}

PHPhotoLibrary.requestAuthorization { status in
guard status == .authorized else { return }

// Use PHPhotoLibrary.shared().performChanges(...) to add assets.
}
}

func setupCaptureSession() {
let captureSession = AVCaptureSession()
// Connect inputs and outputs to the session
captureSession.beginConfiguration()
let videoDevice = AVCaptureDevice.default(.builtInWideAngleCamera,
for: .video, position: .unspecified)
guard
let videoDeviceInput = try? AVCaptureDeviceInput(device: videoDevice!),
captureSession.canAddInput(videoDeviceInput)
else { return }
captureSession.addInput(videoDeviceInput)
photoOutput = AVCapturePhotoOutput()
guard captureSession.canAddOutput(photoOutput) else { return }
captureSession.sessionPreset = .photo
captureSession.addOutput(photoOutput)
captureSession.commitConfiguration()
// Display a camera preview
let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
previewLayer.frame = view.bounds
previewLayer.videoGravity = .resizeAspectFill
view.layer.addSublayer(previewLayer)
// Run the capture session
captureSession.startRunning()
}

// MARK: - Photo capture

func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
// guard error != nil else { print("Error capturing photo: \(error!)"); return }

self.stillImageData = photo.fileDataRepresentation()

performSegue(withIdentifier: "showDetail", sender: self)
}

// MARK: - Navigation

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showDetail" {
let controller = (segue.destination as! UINavigationController).topViewController as! RootViewController
controller.detailItem = stillImageData
}
}

}


import UIKit
import Photos

class RootViewController: UIViewController {

@IBOutlet weak var imageView: UIImageView!

func configureView() {
if let stillImageData = detailItem {
imageView.image = UIImage(data: stillImageData)
}
}

var detailItem: Data? {
didSet {
configureView()
}
}

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
}

@IBAction func dismissViewController(_ sender: Any) {
dismiss(animated: true, completion: nil)
}

@IBAction func savePhotoToPhotosLibrary(_ sender: Any) {
PHPhotoLibrary.requestAuthorization { status in
guard status == .authorized else { return }

PHPhotoLibrary.shared().performChanges({
// Add the captured photo's file data as the main resource for the Photos asset.
let creationRequest = PHAssetCreationRequest.forAsset()
creationRequest.addResource(with: .photo, data: self.detailItem!, options: nil)
}, completionHandler: { success, error in
if !success { NSLog("error creating asset: \(error!)") }
})
}
}
/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/

}

如果有人能伸出援手,我们将不胜感激。1) 为什么,如果 stillImageDatadetailItem 被分配了非 nil 值,它们是否为 nil,以及 2) 为什么 if-let 子句执行它的 block ?

最佳答案

stillImageDatadetailItem 不是 nil,但是 imageViewnil

prepare(for 执行 controller.detailItem = stillImageData 时,目标 Controller 的 View 尚未加载,因此所有 socket 均未连接。当在 configureView() 中访问 socket 时发生崩溃。

在这种情况下调用viewDidLoad中的configureView()

var detailItem: Data? 

override func viewDidLoad() {
super.viewDidLoad()
configureView()
}

另一方面,如果 detailItem 将要更新多次,请检查 outlet

var detailItem: Data? {
didSet {
if imageView != nil { configureView() }
}
}

override func viewDidLoad() {
super.viewDidLoad()
configureView()
}

关于ios - 困惑 : Non-nil value unwrapped nil, if-let 子句在 nil 值上执行 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51688523/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com