local Sandbox cache directorySandbox home directory: PO nshomedirectory ()
/users/spongebob/library/developer/coresimulator/devices/5a46b8a4-f547-4b6a-b5b8-f76a6e13998f/data/containers/ Data/application/02df6b84-32df-4082-a7ae-ef4d9a110e42
Sandbox のbundle (*.app package) path: PO [[NSBundle Mainbundle] Bundlepath]
/users/spongebob/library/developer/coresimulator/devices/5a46b8a4-f547-4b6a-b5b8-f76a6e13998f/data/containers/ Bundle/application/927ab12d-9922-4b00-aab5-2228fd4025a3/01-bundlepath&homedirectory.app
Sandbox のtmp path PO nstemporarydirectory ()
/users/spongebob/library/developer/coresimulator/devices/5a46b8a4-f547-4b6a-b5b8-f76a6e13998f/data/containers/ data/application/02df6b84-32df-4082-a7ae-ef4d9a110e42/tmp/
Sandbox のdocument Path
NSLog (@ "%@", [Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) firstobject]);
/users/spongebob/library/developer/coresimulator/devices/5a46b8a4-f547-4b6a-b5b8-f76a6e13998f/data/containers/ Data/application/21231e28-ed5c-4d6e-ae03-a981ddd33ccc/documents
Note: 5a46b8a4-f547-4b6a-b5b8-f76a6e13998f(simulator) device UUID --Immutable 02df6b84-32df-4082-a7ae-ef4d9a110e42(application) *.app uuid --Randomly generated
OBJC:
Swift:0. Declaration of variables & delayed loading in Swift
@IBOutlet weak var imgview:uiimageview!var str="Https://www.baidu.com/img/bdlogo.png" //in Swift, only one line of "lazy loading" is needed//Reference:http://swiftist.org/topics/129Lazy var Imgcache =nsmutabledictionary ()//lazy var cache:nsmutabledictionary = {//Let dic = Nsmutabledictionary ()// //return dic// }() //mark:lazy Load Array???//lazy var name:string = "Spongebob"//var Arr:nsarray {//get {//return Nsarray ()// }// }
1, network download pictures, cache to local sandbox
///Cache A network picture to a local sandboxfunc Downloadpictocache () {println ("Network Download Pictures") var urlpic= Nsurl (string: STR) var data= NSData (contentsofurl:urlpic!) var image= UIImage (data:data!) Self.imgView.image=Image//Write Sandbox ifData! =Nil {println (str asnsstring). Appendcachedir ()) Data?. WriteToFile ((str asNSString). Appendcachedir (), atomically:true); } }
2. Loading pictures
///Load Network Picturesfunc loadimg () {var img= UIImage (contentsoffile: (str asnsstring). Appendcachedir ())ifSELF.IMGCACHE[STR]! =Nil {println ("loading pictures from memory") Self.imgView.image= Self.imgcache[str] as?UIImage}Else { ifIMG! =Nil {println ("load a picture from a sandbox (disk)") println ((str asnsstring). Appendcachedir ())//add a picture to the picture buffer poolSelf.imgCache.setObject (img!, forkey:str) self.imgView.image=img}Else{Downloadpictocache () }}}
3, here for NSString wrote a classification (in order to splice the sandbox path address)
/*/// Splicing Document Directory-(NSString *) appenddocumentdir;///Mosaic cache Directory-(NSString *) appendcachedir;///splicing temporary directory-(NSString *) appendtmpd IR;*/-(NSString *) Appenddocumentdir {nsstring*dir =[Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) lastobject]; //stringbyappendingpathcomponent when stitching, will automatically add a backslash "/"//Self is the string itself, Lastpathcomponent gets the last string of the file path! return[dir stringByAppendingPathComponent:self.lastPathComponent];}-(NSString *) Appendcachedir {nsstring*dir =[Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES) lastobject]; return[dir stringByAppendingPathComponent:self.lastPathComponent];}-(NSString *) Appendtmpdir {return[Nstemporarydirectory () stringByAppendingPathComponent:self.lastPathComponent];}
Source sharing: https://github.com/SpongeBob-GitHub/Sandbox_Caches
Swift Lazy Loading , reference:http://swiftist.org/topics/129
"Swift" Sandbox cache