NSData and Nsfilemanager examples in iOS micro-solution

Source: Internet
Author: User

NSData follows the Nscopying nscoding protocol, which provides object-oriented array storage as bytes

A buffer is required for reading and writing files, and NSDate provides such a cache.

Defines a string of type Char

const char * string = "Hi There, this is a C string";

Create buffers and add strings to them

NSData * data = [NSData datawithbytes:string length:strlen (String) +1];

Output

NSLog (@ "Data is%@", data);

NSLog (@ "%lu bytes string is '%s '", [data length],[data bytes]);

Define a string, save a path

NSString * Path = @ "/tmp/ver.txt";

Save the string of this save path to another file encoding is encoded

[Path writetofile:@ "/tmp/string.txt" Atomically:yes encoding:nsasciistringencoding Error:nil];

Add an array and add a few strings

Nsarray * phrase;

phrase = [Nsarray arraywithobjects:@ "I", @ "good", @ "seem", @ "to", nil];

Writes the array (the string path defined above) in the file

[Phrase Writetofile:path atomically:yes];

Print

NSLog (@ "%@", phrase);

Creating a File Manager

/*nsfilemanager can be used to query the Word Library directory, create, rename, delete directories, and get/Set the file properties of the method */

NSFILEMANAGER * FM;

FM = [Nsfilemanager Defaultmanager];

Create a buffer, use the Nsfilemanager object to get the contents of the file, that is, the properties of this file can be modified

NSData * FILEDATA;

FILEDATA = [FM contentsatpath:@ "/tmp/ver.txt"];

Print

NSLog (@ "file data is%@", fileData);

To judge the NSData object

if (FileData)

{

NSLog (@ "file read success");

}

Else

{

NSLog (@ "file read failed");

}

Defines an object of a Boolean type

BOOL ifsucess;

In the Objective-c species, right is yes error is no

ifsucess = NO;

Gets the contents of the file obtained by the Nsfilemanager object in the Filedata object above, then creates a new path and stores

ifsucess = [FM createfileatpath:@ "/tmp/test4.txt" Contents:filedata Attributes:nil];

To judge a Boolean object

if (ifsucess)

{

NSLog (@ "Create file sucess");

}

Else

{

NSLog (@ "Create file failed");

}

NSData and Nsfilemanager examples in iOS micro-solution

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.