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