getResourceValue + NSURLIsDirectoryKey fail

Discussion in 'iOS Programming' started by gameplayerxp, Feb 25, 2011.

  1. gameplayerxp macrumors newbie

    gameplayerxp

    Joined:
    Dec 1, 2010
    Location:
    Sydney
    #1
    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?
     
  2. TheFuel macrumors newbie

    TheFuel

    Joined:
    Feb 8, 2008
    Location:
    Bothell, WA (USA)
    #2
    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.
     
  3. gameplayerxp thread starter macrumors newbie

    gameplayerxp

    Joined:
    Dec 1, 2010
    Location:
    Sydney
    #3
    But the doc says:

    And I'm using 4.0 simulator.

    Is there any alternative on prior 4.0 iOS?
     
  4. TheFuel macrumors newbie

    TheFuel

    Joined:
    Feb 8, 2008
    Location:
    Bothell, WA (USA)
    #4
    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.
     
  5. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #5
    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.
     
  6. TheFuel macrumors newbie

    TheFuel

    Joined:
    Feb 8, 2008
    Location:
    Bothell, WA (USA)
    #6
    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.
     
  7. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #7
    I get these for a file


     
  8. TheFuel macrumors newbie

    TheFuel

    Joined:
    Feb 8, 2008
    Location:
    Bothell, WA (USA)
    #8
    You'll note that UTI is not included. NSFileType is a low-level abstraction of a file system type. UTI describes the content of the file.
     
  9. gameplayerxp, Mar 4, 2011
    Last edited by a moderator: Mar 4, 2011

    gameplayerxp thread starter macrumors newbie

    gameplayerxp

    Joined:
    Dec 1, 2010
    Location:
    Sydney
    #9
    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:

    Thanks. I'll give it a try.
     

Share This Page