![](/img/trans.png)
[英]Initializer for conditional binding must have Optional type, not 'String' - ios - swift
[英]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.