Thursday, May 9, 2013

NSDictionary NSFileImmutable example ios


fileIsImmutable - NSFileImmutable

Returns the value for the NSFileImmutable key.
- (BOOL)fileIsImmutable
Return Value
The value for the NSFileImmutable key, or NO if the dictionary doesn’t have an entry for the key.
Discussion
This and the other file... methods are for use with a dictionary, such as those returned from the methodsfileAttributesAtPath:traverseLink: (NSFileManager), directoryAttributes(NSDirectoryEnumerator), and fileAttributes (NSDirectoryEnumerator), that represents the POSIX attributes of a file or directory.
Example of [NSDictionary NSFileImmutable]
NSDictionary* attribs = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:NSFileImmutable];
[[NSFileManager defaultManager]  setAttributes: attribs ofItemAtPath:@"/path/to/file" error:nil];
Example of [NSDictionary NSFileImmutable]
NSDictionary* att = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:[NSNumber numberWithBool:YES], nil] forKeys:[NSArray arrayWithObjects:@"NSFileImmutable", nil]];
[filemgr createDirectoryAtPath:@"YourPath" withIntermediateDirectories:YES attributes:nil error:nil];
[filemgr createFileAtPath:@"YourPath/yourFile.txt" contents:data attributes:att];
Example of [NSDictionary NSFileImmutable]
+ (BOOL)isDocumentLocked:(NSDocument*)doc
{
  if (doc == nil)
  {
    return NO;
  }
  else if ([doc respondsToSelector:@selector(isLocked)]) // 10.8
  {
    return [doc isLocked];
  }
  else // OS X version < 10.8
  {
    NSError * error;
    BOOL isAutosavingSafe = [doc checkAutosavingSafetyAndReturnError:&error];
    if (!isAutosavingSafe)
    {
      return YES;
    }

    if (doc.fileURL == nil)
      return NO;

    NSFileManager* fm = [NSFileManager defaultManager];
    NSString* path = doc.fileURL.absoluteURL.path;

    if (![fm isWritableFileAtPath:path])
      return YES; // No writing permissions

    NSDictionary *attributes = [fm attributesOfItemAtPath:path error:&error];
    BOOL isLocked = [[attributes objectForKey:NSFileImmutable] boolValue];
    if (isLocked)
    {
      return YES;
    }
  }
  return NO;
}