簡體   English   中英

Swift - 條件綁定的初始化程序必須具有可選類型,而不是 'PHFetchResult<phasset> '</phasset>

[英]Swift - Initializer for conditional binding must have Optional type, not 'PHFetchResult<PHAsset>'

將 Xcode 更新到 12.3 后,我收到錯誤“條件綁定的初始化程序必須具有可選類型,而不是 'PHFetchResult'”,它在以前的版本中按預期工作。

func fetchRecentPhotos() {
    
    if !self.recentImagesArray.isEmpty{return}
    DispatchQueue.global(qos: .default).async {
        let fetchOptions = PHFetchOptions()
        fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
        
        if let fetchResult : PHFetchResult = PHAsset.fetchAssets(with: .image, options: fetchOptions){
            if fetchResult.count > 0 {
                self.recentImagesArray.removeAll()
                for i in 0..<fetchResult.count {
                    let asset =  fetchResult.object(at: i)
                    self.recentImagesArray.append(RecentImage(asset: asset))
                }
                DispatchQueue.main.async {
                    if !self.isVideoStarted{
                        self.recentImagesCollectionView.reloadData()
                        self.recentMediaCollectionHeight.constant = 100
                        print("\(Date())fetchRecentPhotos ===== done")
                        if !self.isMultipleSelection{
                            self.setupGesturesForCameraSelection()
                        }
                        UIView.animate(withDuration: 0.2, animations: {
                            self.view.layoutIfNeeded()
                        })
                    }
                }
            }else{
                print("you got no photos")
            }
        }
    }
    }

有沒有人解決了這個問題?

在此處輸入圖像描述

請快速查看文檔fetchAssets返回一個非可選的,因此您不能使用if let

代替

if let fetchResult : PHFetchResult = PHAsset.fetchAssets(with: .image, options: fetchOptions){ ... }

let fetchResult = PHAsset.fetchAssets(with: .image, options: fetchOptions)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM