[英]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.