Sunday, June 16, 2013

NSFileWrapper fileAttributes example in Objective C (iOS).


NSFileWrapper fileAttributes

Returns a file wrapper’s file attributes.

- (NSDictionary *)fileAttributes

Return Value of [NSFileWrapper fileAttributes]
File attributes, in a dictionary of the same sort as that returned by attributesOfItemAtPath:error: (NSFileManager).

NSFileWrapper fileAttributes example.
- (BOOL) contentsOfFileWrapper:(NSFileWrapper*)fileWrapper equalContentsAtPath:(NSString*)path {       
    NSDictionary *fileAttrs = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil];
    NSUInteger fileSize = [attrs fileSize];
    NSUInteger fileWrapperSize = [fileWrapper.fileAttributes fileSize];  // Will return zero if the file wrapper hasn't been written
    if (fileWrapperSize > 0 && fileSize != fileWrapperSize) return NO;

    NSData *fileData = [NSData dataWithContentsOfURL:fileURL];
    NSData *fileWrapperData = fileWrapper.regularFileContents;
    return [fileData isEqualToData:resourceData];
}

End of NSFileWrapper fileAttributes example article.