本文介绍了类型“AnyObject”不符合协议“SequenceType”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  func loadThumbnails(){

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

thumbnails = [QSPhotoInfo]()

for item:目录内容中的AnyObject {
让fileName = item作为NSString
如果fileName.hasPrefix(kThumbnailImagePrefix ){
let image = loadImageFromDocumentsDirectory(fileName)
var photoInfo = QSPhotoInfo()
photoInfo.thumbnail = image;
photoInfo.thumbnailFileName = fileName
thumbnails + = photoInfo
}
}
}

编译错误如下:

这是什么菜单?

谁可以帮助我,很多!!!!

解决方案

苹果在:

现在, directoryContent 只符合协议 AnyObject ,所以你不能在它上面使用for循环。如果你想这样做,你必须做类似于以下内容的:

 对于directoryContent中的项为[AnyObject] { 
//做东西
}


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
        }
    }
}

the compile error is below:

what does this menas?

who can help me ,thks a lot!!!!

解决方案

Apple states in The Swift Programming Language:

Right now, directoryContent is just conforming to protocol AnyObject, so you can't use for loops over it. If you want to do so, you have to do something similar to the following:

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

这篇关于类型“AnyObject”不符合协议“SequenceType”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 04:03