Create a folder inside documents folder in iOS apps

后端 未结 9 1041
一生所求
一生所求 2020-11-28 17:51

I just want to create new folders in the documents folder of my iPhone app.

Does anybody know how to do that?

Appreciate your help!

9条回答
  •  一生所求
    2020-11-28 18:28

    Swift 1.2 and iOS 8

    Create custom directory (name = "MyCustomData") inside the documents directory but only if the directory does not exist.

    // path to documents directory
    let documentDirectoryPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first as! String
    
    // create the custom folder path
    let myCustomDataDirectoryPath = documentDirectoryPath.stringByAppendingPathComponent("/MyCustomData")
    
    // check if directory does not exist
    if NSFileManager.defaultManager().fileExistsAtPath(myCustomDataDirectoryPath) == false {
    
        // create the directory
        var createDirectoryError: NSError? = nil
        NSFileManager.defaultManager().createDirectoryAtPath(myCustomDataDirectoryPath, withIntermediateDirectories: false, attributes: nil, error: &createDirectoryError)
    
        // handle the error, you may call an exception
        if createDirectoryError != nil {
            println("Handle directory creation error...")
        }
    
    }
    

提交回复
热议问题