2010-02-18 97 views
0

如果我創建一個URL一拉:尾隨斜線重要,但剝離?

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

,然後記錄它,看看我有一拉:

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

在結尾的斜線「/文件/」被刪除。如果不重要,不是問題。但是當我做

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

錯誤被拋出。相反,如果我通過一個NSString,其中包括最後的斜線,它沒有錯誤的CFURLHasDirectoryPath,但不會通過一個

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

和Idea這是怎麼回事?理想情況下,我認爲如果我可以在CFURLRef上保留斜線,它會通過錯誤,但我確實不知道。

有什麼想法?

謝謝。

回答

1

CFURLCreateFromFileSystemRepresentation原型是

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

如果你想有一個目錄,通過true到最後一個參數。


此外,使用-stringByAppendingPathComponent:追加路徑組件( 「文檔」),而不是-stringByAppendingString:。前者將爲你處理斜槓。

使用-[NSFileManager fileExistsAtPath:isDirectory:]來檢查一個文件是否真的是一個目錄。

+0

不知道。謝謝! – 2010-02-19 05:35:30