I’m new in objective-c.
I have a path to file contained in an NSString and I want get file size. I found this example and change deprecated code with attributesOfItemAtPath:error: but path is always invalid.
NSFileManager *fileManager = [[NSFileManager alloc] init];
NSString *path = @"~/Library/Safari/History.plist";
NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath: path error: NULL];
if (fileAttributes != nil) {
NSNumber *fileSize;
if (fileSize == [fileAttributes objectForKey:NSFileSize]) {
NSLog(@"File size: %qi\n", [fileSize unsignedLongLongValue]);
}
}
else {
NSLog(@"Path (%@) is invalid.", pPath);
}
[NSFileManager release];
This should work:
It’s very similar to the one used by you, but in yours there’s a mistake: you put
NULLinstead ofnilin theerror:handling.Make sure also to expand tilde in your path, as explained in the documentation: use
stringByExpandingTildeInPath, so yourNSString *pathshould be something like this:Here you can find some explanations about the difference between
nilandNULL.