Swift - 条件绑定的初始化程序必须具有可选类型,而不是 'PHFetchResult<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")
}
}
}
}
有人解决了这个问题吗?
请快速查看 documentation:fetchAssets
returns 是非可选的,因此您不能使用 if let
。
替换
if let fetchResult : PHFetchResult = PHAsset.fetchAssets(with: .image, options: fetchOptions){ ... }
和
let fetchResult = PHAsset.fetchAssets(with: .image, options: fetchOptions)
将 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
returns 是非可选的,因此您不能使用 if let
。
替换
if let fetchResult : PHFetchResult = PHAsset.fetchAssets(with: .image, options: fetchOptions){ ... }
和
let fetchResult = PHAsset.fetchAssets(with: .image, options: fetchOptions)