我的 Mac 的 WebServer 文件夹中有一个 URL 文件。
当我想用我的本地主机 ( http://192.168.1.95 ) 读取这样的文件时,它起作用了:
NSURL *numberOfAccounts = [NSURL URLWithString:@"http://192.168.1.95/NumberOfAccounts"];
NSString *text = [NSString stringWithContentsOfURL:numberOfAccounts encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@",text); // it works
但是当我想检查文件是否存在时,总是返回NO
:
BOOL isDir;
if ([[NSFileManager defaultManager] fileExistsAtPath:numberOfAccounts.path isDirectory:&isDir]) {
NSLog(@"Yes"); // never call
我尝试使用 .path
或 .absoluteString
,使用直接的 NSString @"http://192.168.1.95/NumberOfAccounts"
,使用函数 fileURLWithPath
,但它不起作用,我不明白,因为 NSLog 中的路径与我要读取文件时的路径相同。
此外,我想当我想列出一个文件夹的包含时我有同样的路径问题,因为我的 NSSMutableArray 是空的:
NSMutableArray *allFiles = [[NSMutableArray alloc] initWithArray:[[NSFileManager defaultManager] contentsOfDirectoryAtURL:[NSURL URLWithString:@"http://192.168.1.95"] includingPropertiesForKeys:nil options:NSDirectoryEnumerationSkipsHiddenFiles error:nil]];
提前谢谢你。
最佳答案
fileExistsAtPath
只适用于本地文件系统路径(file://
方案,以/
开头),这也是为什么没有 fileExistsAtURL
方法。
关于ios - fileExistsAtPath 不适用于本地主机 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43435896/