NSData and Nsmutabledata store binary data, which is widely used in file operations, networks, and core graphics images. NSData can no longer be modified after creation, Nsmutabledata can be modified again.
1. Conversion between NSString and NSData NSString *string = @ "Hey, I went to the market!" "; //1, the string is converted intoNSData nsdata *data = [string datausingencoding:nsutf8stringencoding]; //2,NSDataConvert to String nsstring * instring = [[nsstringalloc]initwithdata:Data encoding:nsutf8stringencoding];
2. Conversion between UIImage and NSData //Get Picture Path nsstring *imgpath = [[nsbundle mainbundle] pathforresource:@ " 01loading.png "ofType:nil"; NSData *imgdata = [NSDataDatawithcontentsoffile: Imgpath]; // Direct use, also can write files
Self . ImageView. image = [UIImageimagewithdata: imgdata];1>: Storing pictures in the database store picture path/images/[email protected]2> If the picture is smaller, you can convert the uiimage into a nsdata database.
3, NSNumber, Nsarray, nsdictionary and NSData conversion array to NSData is built on the basis of the archive, also known as serialization, OC NSString objects, NSNumber objects, Nsarray Objects Nsdictionary Dictionary objects, NSData data Objects, and these classes are implemented by nskeyedarchive classes for data storage. The Foundtaion framework <Foundation/NSKeyedArchiver.h> provides a way to archive and archive, also known as the process of serialization and deserialization. 1> To save a dictionary or array directly to a file in an archived form //Initialize a dictionary nsdictionary *dict =@{
@ "Key1":@ "HAHAHAH1",
@ "Key2":@ "HAHAHAH2",
@ "Key3":@ "Hahahah3",
@ "Key4":@ "Hahahah4",
@ "Key5":@ "Hahahah5",
@ "Key6":@ "Hahahah6", @ "Key7":@ "Hahahah7"}; //Archive Path nsstring *docpath = [[nssearchpathfordirectoriesindomains(nsdocumentdirectory, Nsuserdomainmask, YES) firstobject] stringbyappendingpathcomponent:@ " Dict.plist "]; //save directly to file after archiving[nskeyedarchiverarchiverootobject:d ICT tofile:d Ocpath];
2> Convert a dictionary or array to nsdata before saving to a file //Initialize a dictionary
nsdictionary *dict =@{
@ "Key1":@ "HAHAHAH1",
@ "Key2":@ "HAHAHAH2",
@ "Key3":@ "Hahahah3",
@ "Key4":@ "Hahahah4",
@ "Key5":@ "Hahahah5",
@ "Key6":@ "Hahahah6", @ "Key7":@ "Hahahah7"}; //Archive Path nsstring *docpath = [[nssearchpathfordirectoriesindomains(nsdocumentdirectory, Nsuserdomainmask, YES) firstobject] stringbyappendingpathcomponent:@ " Dict.plist "]; // convert to NSData NSData*dictdata = [NskeyedarchiverArchiveddatawithrootobject:d ICT]; // save to file
Nsfilemanager*FM = [NsfilemanagerDefaultmanager];[FM createfileatpath:d ocpath contents:d ictdataattributes:nil];3> the archive directly from the file nsdictionary *undict = [NskeyedunarchiverUnarchiveobjectwithfile:d Ocpath]; Convert files to NSData, archive from NSData NSData *undictdata = [FMContentsatpath:d Ocpath]; nsdictionary *undict = [Nskeyedunarchiver unarchiveobjectwithdata:undictdata];
NSData and Nsmutabledata