how to use contentsOfDirectory

I've been trying to use contentsOfDirectory to access the MOV files in the documents folder. However it won't work. Here's my script:


  1. let docDir = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
  2. let filePath = try! FileManager.default.contentsOfDirectory(at: docDir, includingPropertiesForKeys: nil)



I get the error message "Cannot use instance member 'docDir' within property initializer; propery initializers run before 'self' is available" in my filePath variable. Why am I getting this?

Accepted Reply

Are you sure you're not just seeing the first part of a longer string when you print? It works fine for me in a playground.


Note that your "filePath" variable isn't a file path. It's actually an array of URLs returned from "contentsOfDirectory".

You need to do some actual debugging to find out what's going on. What is "filePath.count" (the number of elements in the array)? Does your document directory actually contain only one item, which is a subdirectory containing the files you really want?

Replies

From the error message, we know that "docDir" is an instance property, so your code fragment is really something like this:


class *** {
     let docDir = …
     let filePath = …
     …
}


That is, this is a class definition, and each instance of class *** (whatever it is) has its own value of instance properties "docDir" and "filePath". When you have initialization expressions for instance properties, the expressions are not allowed to refer to other instance properties, such as filePath referring to docDir here. This is a consequence of Swift's rules of initialization, which exist to ensure that class instances are initialized in a safe, deterministic way.


The real question is when you want to access the contents of the documents directory. Obviously, being part of the file system, the directory can have different contents at different times. Do you want to get its contents:


(a) Once when your app starts up?


(b) Every time a *** instance is created?


(c) When you call a specific method of an *** instance?


Depending on your answer, there will be different solutions to the problem you're having.

Hey Quncy,


I'm trying to get it's contents on app startup, so that I can pass any mov files in it to a cell which will play it on tap. I also need to have a refresh button that can activate it (prob obvious). So I'm thinking that I'll have a function that will search all files within the documents, find what isn't assigned a cell, and make an array of that list (in case there's multiple). That array will be read by another function which will read the first file and send that to my cell class, end it's life, and run again to search for any other non-assigned files. Right??

OK, finally was able to get it to access it by putting my second statement in the ViewDidLoad function. However, it only sees the the filrst file. I've put "print filePath" in there to see. Why is that?

Are you sure you're not just seeing the first part of a longer string when you print? It works fine for me in a playground.


Note that your "filePath" variable isn't a file path. It's actually an array of URLs returned from "contentsOfDirectory".

You need to do some actual debugging to find out what's going on. What is "filePath.count" (the number of elements in the array)? Does your document directory actually contain only one item, which is a subdirectory containing the files you really want?

(face slap) (face slap again) I see it now....thanks.