Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 1039 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Type 'AnyObject' does not conform to protocol 'SequenceType'

#1
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'

what does this menas?

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

#2
Apple states in [The Swift Programming Language][1]:

> The for-in loop performs a set of statements for each item in a range,
> sequence, collection, or progression.

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
}


[1]:

[To see links please register here]

Reply

#3
`contentsOfDirectoryAtPath` returns an `NSArray`, whereas you are casting it to `AnyObject`. The solution is to cast it to either `[AnyObject]?` or `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:

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:

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

or

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



Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through