Swift-Common file directory path acquisition (home directory, document directory, cache directory, etc.)

Source: Internet
Author: User
Tags sqlite database

You are here: Home> Programming Communities> Swift> Swift-Common file directory path acquisition (Home directory, document directory, cache directory, etc.) Swift-Common file directory path acquisition (Home directory, document directory, cache directory, etc.) 2015-06- 15 16:06 Release: yuhang Browse: 207
The iOS application can only operate files in its own directory, and cannot access other storage spaces. This area is called a sandbox. Here are the commonly used program folder directories:


1, Home directory ./ The directory where the documents of the entire application are located
1 2 // Get the home directory of the program let homeDirectory = NSHomeDirectory ()
2. The Documnets directory ./Documents user document directory, Apple recommends that the file data created in the program or browsed in the program is saved in this directory, and this directory will be included when iTunes is backed up and restored
1 2 3 4 5 6 7 // Method 1 let documentPaths = NSSearchPathForDirectoriesInDomains (NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true) let documnetPath = documentPaths [0] as! String // Method 2 let ducumentPath2 = NSHomeDirectory () + "/ Documents "
3. Library directory ./Library There are two subdirectories under this directory: Caches and Preferences Library / Preferences directory, which contains the application's preference setting files. Instead of creating a preference file directly, you should use the NSUserDefaults class to get and set application preferences. Library / Caches directory, mainly store cache files, iTunes will not back up this directory, files in this directory will not be deleted when you exit
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 // Library directory-method 1 let libraryPaths = NSSearchPathForDirectoriesInDomains (NSSearchPathDirectory.LibraryDirectory, NSSearchPathDomainMask.UserDomainMask, true) let libraryPath = libraryPaths [0] as! String // Library directory-method 2 let libraryPath2 = NSHomeDirectory () + "/ Library" // Cache directory-method 1 let cachePaths = NSSearchPathForDirectoriesInDomains (NSSearchPathDirectory.CachesDirectory, NSSearchPathDomainMask.UserDomainMask, true) let cachePath = cachePaths [0] as! String // Cache Directory-Method 2 let cachePath2 = NSHomeDirectory () + "/ Library / Caches"
4. The tmp directory ./tmp is used to store temporary files, save unnecessary information when the application is started again, and clear after restart.
1 2 3 4 5 // Method 1 let tmpDir = NSTemporaryDirectory () // Method 2 let tmpDir2 = NSHomeDirectory () + "/ tmp"

5, the directory where the program is packaged and installed NSBundle.mainBundle ()
After the project is packaged and installed, it will be in the NSBundle.mainBundle () path, which is read-only and cannot be modified.
So when we have a SQLite database in our project, when the program starts, we can copy the database under this path to the Documents path, and the entire project will operate the database under the Documents path in the future.

1 2 3 4 5 6 7 8 9 // Declare a path under Documents var dbPath = NSHomeDirectory () = "/Documents/hanggeDB.sqlite" // Determine whether the database file exists if! NSFileManager.defaultManager (). FileExistsAtPath (dbPath ) {// Get the database path in the installation package var bundleDBPath: String? = NSBundle.mainBundle (). PathForResource ("hanggeDB", ofType: "sqlite") // Copy the database in the installation package to the NSFileManager.defaultManager (Documents directory ) .copyItemAtPath (bundleDBPath !, toPath: dbPath, error: nil)}
Swift-common file directory path acquisition (Home directory, document directory, cache directory, etc.)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.