开发者

Trailing slash important, but stripped out?

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 sla开发者_JS百科sh on the CFURLRef, it'll pass the errors, but I don't know that for sure.

Any thoughts?

Thanks.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜