2017-04-03 19 views

回答

3

NSURL沒有辦法讓你看看另一個NSURL是否代表另一個的根路徑。

一個可能的解決方案是使用path屬性將這兩個URL轉換爲路徑字符串。然後看看一個字符串是否是另一個的前綴。但在獲取URL的路徑之前,請同時使用URLByStandardizingPathURLByResolvingSymlinksInPath以確保一致的結果。

例子:

NSURL *specialURL = ... // a URL for one of the special parent directories 
NSURL *fileURL = ... // a URL for one of the files to check 
NSString *specialPath = [specialURL.URLByStandardizingPath.URLByResolvingSymlinksInPath.path stringByAppendingString:@"/"]; 
NSString *filePath = fileURL.URLByStandardizingPath.URLByResolvingSymlinksInPath.path 
if ([filePath hasPrefix:specialPath]) { 
    // This file is in this special directory 
} 
+2

您需要的前綴測試或其他類似'/ super'將被視作'/超市/ qfc'的父東西前一個加上尾部斜槓'specialPath'。 ('NSURL'''''''''''''' – CRD

+0

@CRD好點,謝謝。更新。奇怪的是,當您打印代表文件夾的文件路徑的「NSURL」時,會顯示尾部斜線。但'path'的結果不包含斜線。 – rmaddy

+0

謝謝!這似乎工作得很好:) –

相關問題