How to create a folder or directory in the documentdirectory iOS swift?

Solution:

Here the below code for creating a folder inside the file directory.The below code is used for creating sounds folder inside filedirectory.


let fileManager = FileManager.default
           
            let documentDirectoryURL = FileManager.default.urls(for: .libraryDirectory, in: .userDomainMask).first! as NSURL
            var newUrl:NSURL!
            let newDir = documentDirectoryURL.appendingPathComponent("Sounds")?.path
           
            do{
                try fileManager.createDirectory(atPath: newDir!,withIntermediateDirectories: true, attributes: nil)
                newUrl = documentDirectoryURL.appendingPathComponent("Sounds") as! NSURL
            } catch {
                print("Error: \(error.localizedDescription)")
            }


If you want to create the same folder in document directory then below the steps

let fileManager = FileManager.default
           
            let documentDirectoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! as NSURL
            var newUrl:NSURL!
            let newDir = documentDirectoryURL.appendingPathComponent("Sounds")?.path
           
            do{
                try fileManager.createDirectory(atPath: newDir!,withIntermediateDirectories: true, attributes: nil)
                newUrl = documentDirectoryURL.appendingPathComponent("Sounds") as! NSURL
            } catch {
                print("Error: \(error.localizedDescription)")
            }

Comments

Popular posts from this blog

Invalid bundle error while upload the app to the app Store

Convert NsNumber, NSDate to String in iOS Swift

Global LocationManager Singleton class iOS Swift