IOS SWIFT Simple Action file

Source: Internet
Author: User

//home Directory
Let homedirectory = Nshomedirectory ()
        
//documents Directory Apple recommends that file data that is created in the program or browsed in the program be saved in this directory, which is included in itunes backup and restore
Let documentpaths = Nssearchpathfordirectoriesindomains (Nssearchpathdirectory.documentdirectory, NSSearchPathD Omainmask.userdomainmask, True)
Let documentpath = documentpaths[0] as! String
//let documentpath = homedirectory + "/documents"
        
//library Directory There are 2 subdirectories under this directory: caches cache, preferences preferences, you should not create preference files directly and use Nsuserdefaults class
Let librarypaths = Nssearchpathfordirectoriesindomains (Nssearchpathdirectory.librarydirectory, NSSearchPathDo Mainmask.userdomainmask, True)
Let LibraryPath = librarypaths[0] as! String
//let LibraryPath = homedirectory + "/library"
        
The //cache directory primarily stores cache files, itunes does not back up this directory, and files in this directory are not deleted when the app exits
Let cachespaths = Nssearchpathfordirectoriesindomains (Nssearchpathdirectory.cachesdirectory, NSSearchPathDomai Nmask.userdomainmask, True)
Let Cachespath = cachespaths[0] as! String
//let Cachespath = homedirectory + "//library/caches"
        
The //tmp directory is used to hold temporary files, keeping information that is not needed during the application restart and emptying after reboot
Let TmpDir = Nstemporarydirectory ()
//let TmpDir = homedirectory + "/tmp"
        
//Operation files
var filemanager = Nsfilemanager.defaultmanager ()
var error:nserror?
        
Let mydir1 = nshomedirectory () + "/documents/myfolder/images"
Let Mydir2 = nshomedirectory () + "/documents/myfolder/films"
Let mydir3 = nshomedirectory () + "/documents/myfolder/musics"
Let mydir4 = nshomedirectory () + "/documents/myfolder/files"
        
//withintermediatedirectories is set to true, which means that all paths in the middle of the directory are created if they do not exist.
var issuccess:bool = Filemanager.createdirectoryatpath (Mydir1, withintermediatedirectories:true, attributes : Nil, Error: &error)
        
Filemanager.createdirectoryatpath (Mydir2, Withintermediatedirectories:true, Attributes:nil, error: &erro R)
Filemanager.createdirectoryatpath (Mydir3, Withintermediatedirectories:true, Attributes:nil, error: &erro R)
Filemanager.createdirectoryatpath (MYDIR4, Withintermediatedirectories:true, Attributes:nil, error: &erro R)
        
//Create files you can write some objects to a file by using the WriteToFile method
var filePath = mydir4 + "/appinfo.txt"
var info = "This is Test text"
info.writetofile (FilePath, Atomically:true, encoding:nsutf8stringencoding, Error: &error)
        
//Save picture
var image = UIImage (named: "2.jpg")
var data:nsdata = uiimagejpegrepresentation (image, 1.0)
var data1:nsdata = uiimagepngrepresentation (image)
Data.writetofile (mydir1 + "/2.jpg", Atomically:true)
Data1.writetofile (mydir1 + "/2.png", Atomically:true)
        
//Save Nsaarry
var array = Nsarray (objects: "111", "222", "333")
Array.writetofile (mydir4 + "/array.plist", Atomically:true)
        
//Save Nsdictionary
var dic = Nsdictionary (objects: ["1111", "2222", "3333"], Forkeys: ["1", "2", "3"])
Dic.writetofile (mydir4 + "/dictionary.plist", Atomically:true)
        
//Determine if the directory exists
var isexist = Filemanager.fileexistsatpath (FilePath)
        
//Move Files
var filePath2 = mydir1 + "/appinfo.txt"
Filemanager.moveitematpath (FilePath, Topath:filepath2, Error: &error)
        
//move, rename
var filePath3 = mydir1 + "/appinfo2.txt"
Filemanager.moveitematpath (filePath2, Topath:filepath3, Error: &error)
        
//Copy, rename
var filePath4 = mydir1 + "/appinfocopy.txt"
Filemanager.copyitematpath (filePath3, Topath:filepath4, Error: &error)
        
//Get all files under directory
var filearray = Filemanager.subpathsatpath (MYDIR1)
//println (Filearray)
        
//Get the file attributes
var fileattributes: [nsobject:anyobject]? = Nsfilemanager.defaultmanager (). Attributesofitematpath (filePath2 , error: &error)
var modificationdate = fileattributes! [Nsfilemodificationdate] As! String
println (modificationdate)//2015-09-03 12:04:39 +0000
        
//delete files
Filemanager.removeitematpath (FILEPATH4, Error: &error)
        
//delete all files under directory
//Method 1
var fileArray2: [anyobject]? = Filemanager.subpathsatpath (MYDIR1)
For F in filearray2! {
Filemanager.removeitematpath (Mydir1 + "/\ (f)", Error: &error)
        }
//Method 2
Filemanager.removeitematpath (MYDIR1, Error: &error)
Filemanager.createdirectoryatpath (Mydir1, Withintermediatedirectories:true, Attributes:nil, error: &erro R)

IOS SWIFT Simple Action file

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.