Tutorial :Trailing slash important, but stripped out?



Question:

If I create a url a la:

const UInt8 *pFilepath = (const UInt8 *)[[NSHomeDirectory() stringByAppendingString:@"/Documents/"] UTF8String];  CFURLRef ldestination = CFURLCreateFromFileSystemRepresentation (NULL, pFilepath, strlen((const char*)pFilepath), false);  

and then log it to see what I've got a la:

NSLog(@"destination url:%@",(NSString*)ldestination);  

the trailing slash on "/Documents/" is removed. Not a problem if it's not important. But when I do

dirPath = CFURLHasDirectoryPath(ldestination);  if (!dirPath) {      fprintf(stderr, "no dice");      return false;  }  

the error is thrown. If instead, I pass an NSString which includes the trailing slash, it doesn't error on the CFURLHasDirectoryPath, but won't pass a

writeStream = CFWriteStreamCreateWithFile(kCFAllocatorDefault, ldestination);  assert(writeStream != NULL);  

And idea what's going on? Ideally, I think that if I can keep the trailing slash on the CFURLRef, it'll pass the errors, but I don't know that for sure.

Any thoughts?

Thanks.


Solution:1

The prototype of CFURLCreateFromFileSystemRepresentation is

CFURLRef CFURLCreateFromFileSystemRepresentation (     CFAllocatorRef allocator,     const UInt8 *buffer,     CFIndex bufLen,     Boolean isDirectory    // <------ note this  );  

If you want a directory, pass true to the last parameter.


Also, use -stringByAppendingPathComponent: to append path components ("Documents") instead of -stringByAppendingString:. The former will deal with the slashes for you.

Use -[NSFileManager fileExistsAtPath:isDirectory:] to check whether a file is really a directory.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »