Retrieve Attributes From The File In iPhone

In this application ,we will learn how to retrieve and set specific file/directory  attributes. We have been specifying nil for the dictionary attributes of files and directories, specify  a dictionary  which provides attributes that are different from the default.We can alter the attributes of a file system object after it has been created.

Step 1: We give an example showing how you can retrieve /set the attributes of a file. We simply define in the main() function of the program.




Step 2:  It starts by creating file.txt file in tmp with a one line text store in it. We first obtain an NSData object from a string by using the NSString’s instance method dataUsingEnc0ding: with utf-8 encoding. We create the file using createFileAtPath:contents:attributes: method.

Step 3: After creating the file using default attributes. To retrieve the file’s attributes of a file, we use the NSFileManager’s instant method fileAttributesAtPath:traverseLink: which we declared already in the code.

Step 4: If  compile and run the application , then we’ll see the output in the Debugger console.

Leave a Comment:

1 comment
bob says February 3, 2011

NSNumber *fSize = [attributes objectForKey:NSFileSize];
NSLog(@”File Size is %qi”,[fSize longLongValue]);

should be written as

unsigned long long fSize = [attributes fileSize];
NSLog(@”File Size is %qu”,fSize);

Add Your Reply