简体   繁体   English

类型'AnyObject'不符合协议'SequenceType'

[英]Type 'AnyObject' does not conform to protocol 'SequenceType'

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: 编译错误如下:

Type 'AnyObject' does not conform to protocol 'SequenceType' 类型'AnyObject'不符合协议'SequenceType'

what does this menas? 这个menas是什么?

who can help me ,thks a lot!!!! 谁可以帮助我,很多!!!!

Apple states in The Swift Programming Language : Apple在Swift编程语言中表示

The for-in loop performs a set of statements for each item in a range, sequence, collection, or progression. for-in循环为范围,序列,集合或进展中的每个项执行一组语句。

Right now, directoryContent is just conforming to protocol AnyObject , so you can't use for loops over it. 现在, directoryContent只是符合协议AnyObject ,所以你不能在它上面使用for循环。 If you want to do so, you have to do something similar to the following: 如果你想这样做,你必须做类似以下的事情:

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

contentsOfDirectoryAtPath returns an NSArray , whereas you are casting it to AnyObject . contentsOfDirectoryAtPath返回一个NSArray ,而您将它转换为AnyObject The solution is to cast it to either [AnyObject]? 解决方案是将它转换为[AnyObject]? or NSArray : NSArray

let directoryContent: [AnyObject]? = fileManager.contentsOfDirectoryAtPath(documentsDirectory, error: &error)

or 要么

let directoryContent: NSArray? = fileManager.contentsOfDirectoryAtPath(documentsDirectory, error: &error)

Then use an optional binding before the for loop: 然后在for循环之前使用可选的绑定:

if let directoryContent = directoryContent {
    for item:AnyObject in directoryContent {

Looking at the contentsOfDirectoryAtPath documentation, it states it always returns an array - so what said above can be reduced to unwrapping the return value to either a swift or objc array, with no need to use the optional binding: 查看contentsOfDirectoryAtPath文档,它声明它总是返回一个数组 - 所以上面说的可以简化为将返回值展开到swift或objc数组,而不需要使用可选的绑定:

let directoryContent: [AnyObject] = fileManager.contentsOfDirectoryAtPath(documentsDirectory, error: &error)!

or 要么

let directoryContent: NSArray = fileManager.contentsOfDirectoryAtPath(documentsDirectory, error: &error)!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型“ AnyObject”不符合协议“ sequenceType” - Type 'AnyObject' does not conform protocol 'sequenceType' 类型'AnyObject'不符合协议'NSFetchRequestResult' - Type 'AnyObject' does not conform to protocol 'NSFetchRequestResult' Swift3错误:类型“ NSFastEnumerationIterator.Element”(又名“ Any”)不符合协议“ AnyObject” - Swift3 error : Type 'NSFastEnumerationIterator.Element' (aka 'Any') does not conform to protocol 'AnyObject' 类型“ ViewController”不符合协议 - Type 'ViewController' does not conform to protocol 类型“ Double”不符合协议“序列类型” - Type 'Double' does not conform to protocol 'Sequence Type' Swift:类型'ViewController'不符合协议'UIPageViewControllerDataSource' - Swift: Type 'ViewController' does not conform to protocol 'UIPageViewControllerDataSource' 类型“类”不符合协议“ MCSessionDelegate” - Type 'Class' does not conform to protocol 'MCSessionDelegate' 类型“SwinjectStoryboardOption”不符合协议“ServiceKeyOption” - Type 'SwinjectStoryboardOption' does not conform to protocol 'ServiceKeyOption' 类型“ WatchManager”不符合协议“ WCSessionDelegate” - Type 'WatchManager' does not conform to protocol 'WCSessionDelegate' 类型'Boolean'不符合协议'BooleanType' - Type 'Boolean' does not conform to protocol 'BooleanType'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM