Quote:
Originally Posted by afixi.ranjeet
I can't understand the purpose of the following code:
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDire ctory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
self.pathToUserCopyOfPlist = [documentsDirectory stringByAppendingPathComponent:@"appData.plist"];
Can someone help me to understand the above?
|
Here is my understanding of what's going on here. Although I am kinda new to this also.
Code:
NSFileManager *fileManager = [NSFileManager defaultManager];
Returns the default NSFileManager object for the file system. So now you have a file manager object that you can use all the file mangager instance methods on. You can find all of those in the API. Stuff like copying and writing and reading and pretty much anything you would want to do with a file.
In the code above, this is just creating a pointer to an NSError Object that you aren't doing anything with. It will probably give you a warning saying it's not being used. But it will still run.
Code:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
This bit creates an array with two entries in it. They each contain a full path (with ~ (~user/) expanded because of the BOOL YES. Then you assign the first entry of the array to the string *documentsDirectory. So now documentsDirectry is a charactor string that is an address to your documents directory. You can see this directory by looking in you iPhone Simulator. Its at ~user/Library/Application Support/iPhone Simulator/User/Applications/ ... Your App (which will be represented by a bunch of numbers)/. Inside there you should see you Documents directory. That is the location you are pointing at.
Code:
self.pathToUserCopyOfPlist = [documentsDirectory stringByAppendingPathComponent:@"appData.plist"];
Here it seem you must have an NSString already created called pathToUserCopyOfPlist. And you are taking the documentsDirectory string from above and sticking a file name on the end of it. This would be a file you are expecting to find in the documents directory. The command stringByAppendingPathComponent is a nice one because it will determine if it needs to stick a "/" slash in the name. If the directory string was to end in "/Documents/" then the string it appends would be just "appData.plist" but if the documents directory ended in just "/Documents" then the string it appends would be "/appData.plist".
But like the post above, this isn't actually doing anything yet. Now you need to tell it what you want to do with your file. If it exists.
Mark A