directoryContentsArray

directoryContentsArray

本文介绍了URL数组抛出可选错误迅速的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定如何解决在以下代码中"if let imageURL = imageFile.path"处发生的可选类型错误.引发的错误是用于条件绑定的初始化程序必须具有可选类型,而不是'String'"

I am not sure how to resolve the optional type error that occurs at "if let imageURL = imageFile.path" in my code below. The error it throws is "Initializer for conditional binding must have Optional type, not 'String'"

在谷歌搜索之后,我猜想这与我在CollectionViewController类的开头设置的directoryContentsArray = URL有关.

After googling, I'm guessing it has something to do with the directoryContentsArray = URL I set at the beginning of my CollectionViewController class.

请帮助!

P.S.抱歉,出现重复的可选错误问题,但我非常困惑:/

P.S. Sorry for the repeat optional error question, but I'm super confused :/

 class CollectionViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

var directoryContentsArray = [URL]()

fileprivate let itemsPerRow: CGFloat = 3
fileprivate let sectionInsets = UIEdgeInsets(top: 50.0, left: 20.0, bottom: 50.0, right: 20.0)

@IBOutlet weak var collectionView: UICollectionView! { didSet {

    collectionView.delegate = self
    collectionView.dataSource = self
    }
}

override func viewDidLoad() {
    super.viewDidLoad()

    func fetchDirectoryContents() {
        let fileManager = FileManager.default
        let documentDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
        let directoryContents = try! fileManager.contentsOfDirectory(at: documentDirectory, includingPropertiesForKeys: nil)

        self.directoryContentsArray = directoryContents
        self.collectionView.reloadData()
    }

    checkPhotoLibraryPermission()

}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    self.collectionView.reloadData()
}

//number of views
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

    return directoryContentsArray.count
        }

//populate the views
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as? myCell {
        let imageFile = self.directoryContentsArray[indexPath.item]
            if let imageURL = imageFile.path,
                imageFile.pathExtension == "jpeg",
                let image = UIImage(contentsOfFile: imageURL) {
                    cell.myImageView.image = image
                } else {
            fatalError("Can't create image from file \(imageFile)")
        }
        return cell
    }
    return UICollectionViewCell()
}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

if let imageURL = info[UIImagePickerControllerImageURL] as? URL {

    let documentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
    do {
        try FileManager.default.moveItem(at: imageURL.standardizedFileURL, to: documentDirectory.appendingPathComponent(imageURL.lastPathComponent))
        collectionView.reloadData()
    } catch {
        print(error)
    }
}

picker.dismiss(animated: true, completion: nil)
}

再次感谢!

推荐答案

URL的path属性的定义是:

The definition of the path property of URL is:

var path: String

因此它不会返回可选值,这意味着您不需要进行let分配.

So it doesn't return an optional which means you don't need to do the let assignment.

只需更改为此:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as? myCell {
        let imageFile = self.directoryContentsArray[indexPath.item]
            if imageFile.pathExtension == "jpeg",
                let image = UIImage(contentsOfFile: imageFile.path) {
                    cell.myImageView.image = image
                } else {
            fatalError("Can't create image from file \(imageFile)")
        }
       return cell
    }
    return UICollectionViewCell()
}

这篇关于URL数组抛出可选错误迅速的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 09:19