一尘不染

类型“ AnyObject”不符合协议“ SequenceType”

swift

func loadThumbnails() {

    let paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)
    let documentsDirectory:NSString = paths[0] as NSString
    var error:NSError?
    let fileManager = NSFileManager()
    let directoryContent:AnyObject = fileManager.contentsOfDirectoryAtPath(documentsDirectory, error: &error)!

    thumbnails = [QSPhotoInfo]()

    for item:AnyObject in directoryContent {
        let fileName = item as NSString
        if fileName.hasPrefix(kThumbnailImagePrefix) {
            let image = loadImageFromDocumentsDirectory(fileName)
            var photoInfo = QSPhotoInfo()
            photoInfo.thumbnail = image;
            photoInfo.thumbnailFileName = fileName
            thumbnails += photoInfo
        }
    }
}

编译错误如下:

类型“ AnyObject”不符合协议“ SequenceType”

这种压力是什么?

谁能帮我很多忙!


阅读 237

收藏
2020-07-07

共1个答案

一尘不染

苹果在Swift编程语言中指出:

for-in循环针对范围,序列,集合或进度中的每个项目执行一组语句。

目前,directoryContent它仅符合protocol
AnyObject,因此您无法在其上使用for循环。如果要这样做,则必须执行类似以下操作:

for item in directoryContent as [AnyObject] {
    //Do stuff
}
2020-07-07