IOS Basic Data Type-nsdata

Source: Internet
Author: User

1, NSData

  Comments:

    The type of data used to store the binary

The NSData class provides a simple way to set a buffer, read the contents of a file into a buffer, or write the contents of a buffer to a file.

Invariant buffers (NSData classes), or variable buffers (Nsmutabledata classes) can also be defined.

  NSData, NSString Mutual transfer:

NSData * data = [str datausingencoding:nsutf8stringencoding]; NSString Convert to NSData type

NSString * Newstr = [[NSString alloc] Initwithdata:data encoding:nsutf8stringencoding];

  NSData, Byte transfer

    NSString *teststring = @ "1234567890";

NSData *testdata = [teststring datausingencoding:nsutf8stringencoding];

byte *testbyte = (BYTE *) [testData bytes];

Byte byte[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23};

NSData *adata = [[NSData alloc] Initwithbytes:byte length:24];

  NSData Turn UIImage

    UIImage *aimage = [UIImage imagewithdata:data];

  NSData Turn Nsdictionary

@interface Nsdictionary (Helpers)

+ (Nsdictionary *) Dictionarywithcontentsofdata: (NSData *) data;

@end

@implementation Nsdictionary (Helpers)

+ (Nsdictionary *) Dictionarywithcontentsofdata: (NSData *) Data {
Cfpropertylistref plist = Cfpropertylistcreatefromxmldata (Kcfallocatordefault, (cfdataref) data,
Kcfpropertylistimmutable,
NULL);
if (plist = = nil) return nil;
if ([(ID) plist Iskindofclass:[nsdictionary class]]) {
return [(Nsdictionary *) plist autorelease];
}
else {
Cfrelease (plist);
return nil;
}
}

@end

NSData Application Scenarios

Reads the contents of a file into a memory buffer

    NSData *fileData;

< Span class= "PLN" > nsfilemanager *filemanager = [[ Nsfilemanager Alloc]init

< Span class= "PLN" >< Span class= "pun" >< Span class= "PLN" > filedata = [filemanager contentsatpath:path

/span> writes the contents of the buffer to a file

nsfilemanager *filemanager = [[nsfilemanager Alloc ]init];

[filemanager Createfileatpath:path contents:fileData Attributes:nil];

< Span class= "PLN" >< Span class= "pun" >< Span class= "PLN" >< Span class= "PLN" >< Span class= "pun" >< Span class= "pun" > World article a big copy of the content according to the following blog finishing

Http://www.cnblogs.com/GISerYang/p/3345350.html

http://bj007.blog.51cto.com/1701577/538990/

http://blog.csdn.net/jerryvon/article/details/7350567

Http://www.cnblogs.com/duchengdong/archive/2012/03/29/2422997.html

IOS Basic Data Type-nsdata

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.