NSData and Nsmutabledata

Source: Internet
Author: User

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&gt: 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&gt 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

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.