【问题标题】:Swift - Initializer for conditional binding must have Optional type, not 'PHFetchResult<PHAsset>'Swift - 条件绑定的初始化程序必须具有可选类型,而不是 'PHFetchResult<PHAsset>'
【发布时间】:2021-01-29 05:56:34
【问题描述】:

将 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")
            }
        }
    }
    }

有人解决了这个问题吗?

【问题讨论】:

  • 不是可选的,你可以直接使用let fetchResult = PHAsset.fetchAssets(with: .image, options: fetchOptions)
  • 谢谢。这就是我现在尝试和工作的。

标签: ios swift xcode phasset phfetchoptions


【解决方案1】:

请快速查看documentationfetchAssets 返回非可选,因此您不能使用if let

替换

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

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

【讨论】:

    猜你喜欢
    • 2018-11-06
    • 1970-01-01
    • 2019-03-11
    • 2017-01-22
    • 2018-03-25
    • 2016-01-08
    • 2017-04-08
    • 2016-09-23
    • 1970-01-01
    相关资源
    最近更新 更多