Showing posts with label writeToFile example. Show all posts
Showing posts with label writeToFile example. Show all posts

Saturday, June 1, 2013

NSData writeToFile example in Objective C (iOS).


NSData writeToFile

Writes the bytes in the receiver to the file specified by a given path.

- (BOOL)writeToFile:(NSString *)path options:(NSDataWritingOptions)mask error:(NSError **)errorPtr

Parameters of [NSData writeToFile]
path
The location to which to write the receiver's bytes.
mask
A mask that specifies options for writing the data. Constant components are described in “NSDataWritingOptions”.
errorPtr
If there is an error writing out the data, upon return contains an NSError object that describes the problem.

Return Value of [NSData writeToFile]
YES if the operation succeeds, otherwise NO.

NSData writeToFile example.
if (data) {   
     NSString *content = [[NSString alloc]  initWithBytes:[data bytes]
                                                      length:[data length] encoding: NSUTF8StringEncoding];

    NSLog(@"%@", content); // verifies data was downloaded correctly

    NSError* error;
    [data writeToFile:storePath options:NSDataWritingAtomic error:&error];

    if(error != nil)
        NSLog(@"write error %@", error);
}

Example of [NSData writeToFile].
NSError *error = nil;
path=@"/Users/aryaxt/Desktop/test2.avi";
[data writeToFile:path options:NSDataWritingAtomic error:&error];
NSLog(@"Write returned error: %@", [error localizedDescription]);

NSData writeToFile example.
-(void)banner:(NSString *)path{
   NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: path]];
   NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);         NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"banner.png"];      
   NSError *writeError = nil;
   [imageData writeToFile:filePath options:NSDataWritingAtomic error:&writeError];

   if (writeError) {
      NSLog(@"Error writing file: %@", writeError);
   }
}

End of NSData writeToFile example article.

NSArray writeToFile example in Objective C (iOS).


NSArray writeToFile

Writes the contents of the array to a file at a given path.

- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)flag

Parameters of [NSArray writeToFile]
path
The path at which to write the contents of the array.
If path contains a tilde (~) character, you must expand it with stringByExpandingTildeInPath before invoking this method.
flag
If YES, the array is written to an auxiliary file, and then the auxiliary file is renamed to path. If NO, the array is written directly to path. The YES option guarantees that path, if it exists at all, won’t be corrupted even if the system should crash during writing.

Return Value of [NSArray writeToFile]
YES if the file is written successfully, otherwise NO.

Discussion of [NSArray writeToFile]
If the array’s contents are all property list objects (NSString, NSData, NSArray, or NSDictionary objects), the file written by this method can be used to initialize a new array with the class method arrayWithContentsOfFile: or the instance method initWithContentsOfFile:. This method recursively validates that all the contained objects are property list objects before writing out the file, and returns NO if all the objects are not property list objects, since the resultant file would not be a valid property list.

NSArray writeToFile example.
//Creating a file path under iOS:
//1) Search for the app's documents directory (copy+paste from Documentation)
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//2) Create the full file path by appending the desired file name
NSString *yourArrayFileName = [documentsDirectory stringByAppendingPathComponent:@"example.dat"];

//Load the array
NSMutableArray *yourArray = [[NSMutableArray alloc] initWithContentsOfFile: yourArrayFileName];
if(yourArray == nil)
{
    //Array file didn't exist... create a new one
    yourArray = [[NSMutableArray alloc] initWithCapacity:10];

    //Fill with default values
}
...
//Use the content
...
//Save the array
[yourArray writeToFile:yourArrayFileName atomically:YES];

Example of [NSArray writeToFile].
if(![array containsObject:dictionary])
{
[array addObject:dictionary];
if(![array writeToFile:path atomically:YES])
{
NSLog(@".plist writing was unsucessfull");
}
     }

NSArray writeToFile example.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *issueURLsPlist = [documentsDirectory stringByAppendingPathComponent:@"test.plist"];

MyClass * myObject = [[MyClass alloc] init];
NSMutableArray * array = [[NSMutableArray alloc] init];
[array addObject:myObject];
[array writeToFile:issueURLsPlist atomically:YES];

End of NSArray writeToFile example article.

Monday, May 20, 2013

NSString writeToFile example ios

writeToFile: atomically: encoding: error:

Writes the contents of the receiver to a file at a given path using a given encoding.
- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFileencoding:(NSStringEncoding)enc error:(NSError **)error
Parameters of [NSString writeToFile]
path
The file to which to write the receiver. If path contains a tilde (~) character, you must expand it with stringByExpandingTildeInPath before invoking this method.
useAuxiliaryFile
If YES, the receiver is written to an auxiliary file, and then the auxiliary file is renamed to path. If NO, the receiver is written directly to path. The YES option guarantees that path, if it exists at all, won’t be corrupted even if the system should crash during writing.
enc
The encoding to use for the output.
error
If there is an error, upon return contains an NSError object that describes the problem. If you are not interested in details of errors, you may pass in NULL.
Return Value
YES if the file is written successfully, otherwise NO (if there was a problem writing to the file or with the encoding).
Discussion of [NSString writeToFile]
This method overwrites any existing file at path.
This method stores the specified encoding with the file in an extended attribute under the name com.apple.TextEncoding. The value contains the IANA name for the encoding and the CFStringEncoding value for the encoding, separated by a semicolon. The CFStringEncoding value is written as an ASCII string containing an unsigned 32-bit decimal integer and is not terminated by a null character. One or both of these values may be missing. Examples of the value written include the following:[NSString writeToFile]
MACINTOSH;0
UTF-8;134217984
UTF-8;
;3071
Example of [NSString writeToFile]
NSError *error = nil;
 BOOL ok = [hello_world writeToFile:filePath atomically:YES 
                        encoding:NSASCIIStringEncoding error:&error];
 if (error) {
      NSLog(@"Fail: %@", [error localizedDescription]);
 }
Example of [NSString writeToFile]
NSString * xmlData = @"This is some random string";
NSString * xmlString = [NSString stringWithString:xmlData];
NSError * error = nil;
if (![xmlString writeToFile:@"data.txt"
                 atomically:NO
                   encoding:NSASCIIStringEncoding
                      error:&error])
{
    NSLog(@"writeToFile failed: %@", error);
}
Example of [NSString writeToFile]
NSString *zStr = [aString stringValue];
NSArray  *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *directory = [paths objectAtIndex:0];
NSString *fileName = @"data.txt";
NSString *filePath = [directory stringByAppendingPathComponent:fileName];
[zStr writeToFile:filePath atomically:YES encoding:NSASCIIStringEncoding error:nil]; 

Thursday, May 9, 2013

NSDictionary writeToFile example ios


writeToFile :atomically:

Writes a property list representation of the contents of the dictionary to a given path.
- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)flag
Parameters
path
The path at which to write the file.
If path contains a tilde (~) character, you must expand it withstringByExpandingTildeInPath before invoking this method.
flag ([NSDictionary writeToFile])
A flag that specifies whether the file should be written atomically.
If flag is YES, the dictionary is written to an auxiliary file, and then the auxiliary file is renamed to path. If flag is NO, the dictionary is written directly to path. The YES option guarantees that path, if it exists at all, won’t be corrupted even if the system should crash during writing.
Return Value
YES if the file is written successfully, otherwise NO.
Discussion of [NSDictionary writeToFile]
This method recursively validates that all the contained objects are property list objects (instances of NSDataNSDateNSNumberNSStringNSArray, or NSDictionary) before writing out the file, and returns NO if all the objects are not property list objects, since the resultant file would not be a valid property list. [NSDictionary writeToFile]
If the dictionary’s contents are all property list objects, the file written by this method can be used to initialize a new dictionary with the class method dictionaryWithContentsOfFile:or the instance method initWithContentsOfFile:.
For more information about property lists, see Property List Programming Guide.
Example of [NSDictionary writeToFile]
NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"myPlistFile.plist"];
if (![fileManager fileExistsAtPath: path])
{
          NSString *bundle = [[NSBundle mainBundle] pathForResource:@”myPlistFile ofType:@”plist”];
          [fileManager copyItemAtPath:bundle toPath:path error:&error];
}
[myDict writeToFile:plistPath atomically: YES];
Example of [NSDictionary writeToFile]
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *plistDirectory = [paths objectAtIndex:0];
NSString *plistPath = [plistDirectory stringByAppendingPathComponent:@"userCompany.plist"];

NSArray *userObjects = [[NSArray alloc] initWithObjects:@"Joe", @"Smith", @"Smith Co", nil];
NSArray *userKeys = [[NSArray alloc] initWithObjects:@"First Name", @"Last Name", @"Company", nil];

NSDictionary *userSettings = [[NSDictionary alloc] initWithObjects:userObjects forKeys:userKeys];

[userSettings writeToFile:plistPath atomically:YES];