From 3ef6bfe5709d136a253206768c36e5ffd91de24f Mon Sep 17 00:00:00 2001 From: Vegard Bieker Matthey Date: Tue, 4 Aug 2026 21:21:36 +0200 Subject: [PATCH] fix: avoid crashing from nil values --- ImageViewer/ViewController.swift | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/ImageViewer/ViewController.swift b/ImageViewer/ViewController.swift index 071e552..81de0b6 100644 --- a/ImageViewer/ViewController.swift +++ b/ImageViewer/ViewController.swift @@ -124,15 +124,17 @@ final class ViewController: UIViewController, UIGestureRecognizerDelegate { if let alert = pendingAlert { present(alert, animated: false, completion: nil) } - if !hasSetContentOffset, let scrollPos = state[currentPath.lastPathComponent]?.scrollPos { + if !hasSetContentOffset && scrollingCollectionView != nil { + guard let path = currentPath else { return } + guard let state = state[path.lastPathComponent] else { return } let screenSize = UIScreen.main.bounds.size - var offset = scrollPos + var offset = state.scrollPos if screenSize.width > screenSize.height { offset.y *= (screenSize.width / screenSize.height) } scrollingCollectionView.setContentOffset(offset, animated: false) hasSetContentOffset = true - } else { + } else if scrollingCollectionView != nil { let screenSize = UIScreen.main.bounds.size var offset = scrollingCollectionView.contentOffset offset.y *= (screenSize.width / screenSize.height) @@ -268,8 +270,8 @@ final class ViewController: UIViewController, UIGestureRecognizerDelegate { && isDirectory.boolValue }.sorted { $0.path < $1.path } - for dir in directories { - queue.async { [self] in + queue.async { [self] in + for dir in directories { if !fileManager.fileExists(atPath: dir.appendingPathComponent(metadataFilename).path) { getMetadataFromFilename(path: dir) if pendingAlert != nil { @@ -885,6 +887,7 @@ final class ViewController: UIViewController, UIGestureRecognizerDelegate { override func viewDidLayoutSubviews() { super.viewDidLayoutSubviews() guard view.bounds.size != lastLayoutSize else { return } + if scrollingCollectionView == nil { return } lastLayoutSize = UIScreen.main.bounds.size if scrollingCollectionView.isHidden || getMode() != .scroll