Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

gameplayerxp

macrumors newbie
Original poster
Dec 1, 2010
23
0
Sydney
I'm using the following code to see if a URL is a folder:

Code:
				NSURL *file = [localFiles objectAtIndex:i];
				NSNumber *isDir;
				NSError* error1 = nil;
				BOOL ret = [file getResourceValue:&isDir forKey:NSURLIsDirectoryKey error:&error1];
				//NSDictionary *properties = [file resourceValuesForKeys:
				//							[NSArray arrayWithObject:NSURLIsDirectoryKey] error:&error1];
				if (ret == NO) {
					NSLog(@"Error when calling getResourceValue: %@", [error1 localizedDescription]);
				}

getResourceValue always return NO and set isDir to nil even file is a URL to a folder.

Is there anything wrong with my code?
 
Nothing wrong with your code. You've stumbled into "Available but not implemented" scenario. Check the doco. Annoying, I hope I saved you some time.
 
Right above that line in the "Discussion" section I suspect you'll find:
This method is unimplemented in iOS, so it performs no operation.

I don't know of an alternative off hand. Be sure to post if you find one.
 
You need to use the path-based NSFileManager APIs. Get the attributes of the item and check the NSFileType attribute for if it's equal to NSFileTypeDirectory.
 
NSFileManager works for a few attributes like query for is directory, fileExistsAtPath:isDirectory:, but a full replacement of getResourceValue:forKey: isn't implemented to my knowledge. In particular obtaining the UTI via NSURLTypeIdentifierKey. In OSX, targets prior to 10.6, LSCopyItemAttribute was used.
 
I get these for a file


{
NSFileCreationDate = "2008-10-30 17:18:42 +0000";
NSFileExtensionHidden = 0;
NSFileGroupOwnerAccountID = 20;
NSFileGroupOwnerAccountName = staff;
NSFileModificationDate = "2011-02-16 15:59:44 +0000";
NSFileOwnerAccountID = 501;
NSFileOwnerAccountName = phoney;
NSFilePosixPermissions = 493;
NSFileReferenceCount = 1;
NSFileSize = 1475441;
NSFileSystemFileNumber = 7821000;
NSFileSystemNumber = 234881026;
NSFileType = NSFileTypeRegular;
}
 
Right above that line in the "Discussion" section I suspect you'll find:


I don't know of an alternative off hand. Be sure to post if you find one.

No. In my Xcode doc's discussion section, the texts are:

Code:
getResourceValue:forKey:error:
Returns the resource value for the property identified by a given key.

- (BOOL)getResourceValue:(id *)valueforKey:(NSString *)keyerror:(NSError **)error

Parameters
value
The value for the property identified by key.
key
The name of one of the URL’s resource properties.
error
The error that occurred in the case that the resource value cannot be retrieved.
Return Value
YES if value is successfully populated; otherwise, NO.

Discussion
value is set to nil if the requested resource value is not defined for the URL. In this case, the method still returns YES.

Availability
Available in iPhone OS 4.0 and later.
See Also
“Common File System Resource Keys”
“File Property Keys”
“Volume Property Keys”
Declared In
NSURL.h

The offline and online version are not same - hate iOS's documentation.:mad:

You need to use the path-based NSFileManager APIs. Get the attributes of the item and check the NSFileType attribute for if it's equal to NSFileTypeDirectory.

Thanks. I'll give it a try.
 
Last edited by a moderator:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.