Showing posts with label NSDictionary. Show all posts
Showing posts with label NSDictionary. Show all posts

Thursday, May 9, 2013

NSDictionary writeToFile atomically 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 atomically])
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 atomically]
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 atomically]
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 atomically]
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 atomically]
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];

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];

NSDictionary valueForKey example ios


valueForKey:

Returns the value associated with a given key.
- (id)valueForKey:(NSString *)key
Parameters
key
The key for which to return the corresponding value. Note that when using key-value coding, the key must be a string (see “Key-Value Coding Fundamentals”).
Return Value
The value associated with key.
Discussion of [NSDictionary valueForKey]
If key does not start with “@”, invokes objectForKey:. If key does start with “@”, strips the “@” and invokes [super valueForKey:] with the rest of the key.
Example of [NSDictionary valueForKey]
NSString *value = [dictionary valueForKey:@"myKey"];
if (!value) value = @"defaultValue";
[myObject setMyString:value];
Example of [NSDictionary valueForKey]
NSDictionary *answer = [self.answers objectAtIndex:indexPath.row];
if ([answer respondsToSelector:@selector(objectForKey)]) {
    cell.textLabel.text = [answer valueForKey:@"answer_id"];
} else {
    [NSException raise:@"Answer is of invalid class" format:@"It should be able to respond to valueForKey, class: %@", [answer class]];
}
Example of [NSDictionary valueForKey]
id answer = [self.answers objectAtIndex:indexPath.row];
if (answer isKindOfClass:[NSDictionary class])
{
  cell.textLabel.text = [answer valueForKey:@"answer_id"];
    } else {
        // I'm ending up here, instead of the cell.textLabel being set
        [NSException raise:@"Answer is of invalid class" format:@"It should be able to respond to valueForKey, class: %@", [answer class]];
    }

NSDictionary objectsForKeys notFoundMarker example ios


objectsForKeys notFoundMarker

Returns the set of objects from the dictionary that corresponds to the specified keys as an NSArray.
- (NSArray *)objectsForKeys:(NSArray *)keys notFoundMarker:(id)anObject
Parameters
keys
An NSArray containing the keys for which to return corresponding values.
anObject
The marker object to place in the corresponding element of the returned array if an object isn’t found in the dictionary to correspond to a given key.
Discussion of [NSDictionary objectsForKeys notFoundMarker]
The objects in the returned array and the keys array have a one-for-one correspondence, so that the nthe object in the returned array corresponds to the nthe key in keys.
Example of [NSDictionary objectsForKeys notFoundMarker]
NSArray *section0Keys = [NSArray arrayWithObjects:@"city", @"phone", @"email", nil];
NSMutableArray *section0 = [[[feedDictionary objectsForKeys:section0Keys notFoundMarker:[NSNull null]] mutableCopy] autorelease];
if ([section0 indexOfObject:[NSNull null]] != NSNotFound) {
    isValid = NO;
}
// you don't need this if you don't care for invalid arrays.
[section0 removeObjectsInArray:[NSArray arrayWithObject:[NSNull null]]];

NSArray *section1Keys = [NSArray arrayWithObjects:@"year", @"degree", nil];
NSMutableArray *section1 = [[[feedDictionary objectsForKeys:section1Keys notFoundMarker:[NSNull null]] mutableCopy] autorelease];
if ([section1 indexOfObject:[NSNull null]] != NSNotFound) {
    isValid = NO;
}
[section1 removeObjectsInArray:[NSArray arrayWithObject:[NSNull null]]];

self.dataArray = [NSArray arrayWithObjects:
section0,
section1,
nil];
Example of [NSDictionary objectsForKeys notFoundMarker]
[[dictionary objectsForKeys:[NSArray arrayWithObject:@"myKey"]
             notFoundMarker:@"defaultValue"]
 objectAtIndex:0]
Example of [NSDictionary objectsForKeys notFoundMarker]
NSMutableArray *objects = [myDictionary objectsForKeys:myKeys notfoundMarker:[NSNull null]];
  for (int ii = 0; ii < [objects count]; ii++ ) {
      if ([objects objectAtIndex:ii] == [NSNull null]) {
          [objects insertObject:[myKeys objectAtIndex:ii] atIndex:ii];
      }
  }