Objective c NSURL路径不工作,但NSString路径工作
我已将一个对象保存到文件中,现在正在尝试检查该文件是否存在。我已经确认了文件的路径,并得出结论,当我将路径硬编码为NSString时,IF语句可以工作,请参见第一块。但是,当我尝试将路径保存为NSURL并将其转换为NSString以便fileManager可以在其上运行其方法时,它不会在路径中找到文件。我在这里遗漏了什么 使用硬代码NSSTRING在此处查找文件:Objective c NSURL路径不工作,但NSString路径工作,objective-c,nsstring,nsurl,nsfilemanager,Objective C,Nsstring,Nsurl,Nsfilemanager,我已将一个对象保存到文件中,现在正在尝试检查该文件是否存在。我已经确认了文件的路径,并得出结论,当我将路径硬编码为NSString时,IF语句可以工作,请参见第一块。但是,当我尝试将路径保存为NSURL并将其转换为NSString以便fileManager可以在其上运行其方法时,它不会在路径中找到文件。我在这里遗漏了什么 使用硬代码NSSTRING在此处查找文件: [NSKeyedArchiver archiveRootObject:employees toFile:@"/Users/x
[NSKeyedArchiver archiveRootObject:employees toFile:@"/Users/xxx/Documents/employees.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *employeesPath = @"/Users/xxx/Documents/employees.plist";
if ([fileManager fileExistsAtPath:employeesPath]) {
NSLog(@"It exists! yes!");
}
else {
NSLog(@"Doesn't exist, sorry bud");
}
未使用NSURL找到文件:
[NSKeyedArchiver archiveRootObject:employees toFile:@"/Users/xxx/Documents/employees.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *employeesPath = [NSURL fileURLWithPath:@"/Users/xxx/Documents/employees.plist"];
NSString *employeesString = [employeesPath absoluteString];
if ([fileManager fileExistsAtPath:employeesString]) {
NSLog(@"It exists! yes!");
}
else {
NSLog(@"Doesn't exist, sorry bud");
}
编辑
--如果我想使用NSURL方法,我可以通过创建一个函数以正确的方式将路径存储到NSString中。这最终奏效了:
NSString* getPropertyListPath() {
NSURL *documentDir = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
NSURL *plist = [documentDir URLByAppendingPathComponent:@"employees.plist"];
return plist.path;
}不要将
NSURL
用作文件路径中介
NSURL *employeesPath = [NSURL fileURLWithPath:@"/Users/xxx/Documents/employees.plist"];
NSString *employeesString = [employeesPath absoluteString];
NSLog(@"employeesString: %@", 'employeesString');
输出:
员工字符串:'file:///Users/xxx/Documents/employees.plist"
这显然不是文件路径。使用NSURL、giuseppe而不是字符串文本是正确的。
NSURL比字符串文本更具兼容性和健壮性。NSURL类型将允许您访问更多的方法和功能 您偶然遇到的技巧是,您需要的文件路径中不包含格式中的“scheme”或“domain” 您可以在NSURL对象上调用
path
方法来检索所需的正确路径格式。path
方法仅将路径组件返回到NSURL地址路径。它不会返回方案或域组件
NSString *correctPathFormat = [yourNsurlObject path];
例如: 如果我在以下目录路径中有一个文件:
NSString* myDirPath = @"/Users/yourUserName/imageFolder";
并将其加载到NSURL对象中:
NSURL *nsurlDirPath = [NSURL fileURLWithPath:myDirPath];
然后附加文件名和文件类型:
NSURL *nsurlFilePath = [nsurlDirPath URLByAppendingPathComponent:@"employee.plist"];
如果调用[nsurlFilePath absoluteString]
方法,将获得格式为“的NSString值”scheme://domain/path"
此操作将注销:
file:///Users/yourUserName/imageFolder/employee.plist
特别注意:这相当于文件路径:
file://localhost/Users/yourUserName/imageFolder/employee.plist
“localhost”只是被省略了,因为默认情况下这是隐含的,所以这就是为什么您在file:///Users/...".
“localhost”是一个别名,指本地设备的ip地址,或者换句话说,指运行代码的设备
最后,要获得所需的正确路径格式,您需要在NSURL对象上运行“path”方法,这将使我们回到我的响应开头的答案:
NSString *correctPathFormat = [nsurlFilePath path];
这将注销正确的“路径”组件,减去“方案”和“域”:
进一步解释: NSURL由三部分组成:
scheme : [http, https, ftp, file]
domain : [www.stackoverflow.com, localhost, ipAddress]
path : [/questions/26663573/, /Users/youUserName/subDirName]
scheme | domain | path
file://localhost/Users/youruserName/file.txt
如果您
NSLog
这两个,您将看到您的两个NSString
不一样。您可以使用[employeesPath path]
。为什么在中间层中使用NSUrl?Caveman调试说,在使用路径之前先记录路径,以防出现问题<代码>NSLog(@“employeesString:%@”,employeesString)回答您的问题,因为这是我被教导的方式,我应该始终使用NSURL存储地址。我仍然在学习它的来龙去脉。谢谢你的回答。谁教你的?您需要理解您编写的每一行代码、id的作用以及为什么需要它。当事情不顺利时,一些时间证明方法。1.想想发生了什么。2.使用调试器跟踪执行并在每个步骤检查变量。3转到caveman并添加临时日志记录语句。仅供参考-如果您确实有一个文件URL,请使用path
方法从URL中额外添加一个实际的文件路径。实际上,我还可以创建一个函数来将路径存储到NSString中。NSString*getPropertyListPath(){NSURL*documentDir=[[NSFileManager defaultManager]URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask ApproverForURL:nil创建:无错误:nil];NSURL*plist=[documentDir URLByAppendingPathComponent:@“employees.plist”];返回plist.path;}
/Users/yourUserName/imageFolder/employee.plist
scheme : [http, https, ftp, file]
domain : [www.stackoverflow.com, localhost, ipAddress]
path : [/questions/26663573/, /Users/youUserName/subDirName]
scheme | domain | path
file://localhost/Users/youruserName/file.txt