问题描述
将 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")
}
}
}
}
有人解决了这个问题吗?
解决方法
请快速浏览一下 documentation:fetchAssets
返回非可选值,因此您不得使用 if let
。
替换
if let fetchResult : PHFetchResult = PHAsset.fetchAssets(with: .image,options: fetchOptions){ ... }
与
let fetchResult = PHAsset.fetchAssets(with: .image,options: fetchOptions)