iOS 如何在 URLByAppendingPathComponent 中添加自定义路径
how to add custom path in URLByAppendingPathComponent in iOS
在我的项目中,我将 jsondata 保存到名为 AllBooking.json 的本地 json 文件中。
代码:
NSURL *fileURL =[[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"AllBooking.json"];
URLByAppendingPathComponent 方法将此文件创建到任何不相关的路径!
和
[自申请文件目录]returns
[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory
inDomains:NSUserDomainMask] lastObject];
有什么方法可以为文件提供自定义路径吗?
嗯,URLByAppendingPathComponent
一切正常。问题出在您的 applicationDocumentsDirectory
方法中。
我刚给你一个运行。这是我的 applicationDocumentsDirectory
方法的样子(假设您想要 return URL 用于文档目录;但是为了清楚起见,方法名称必须以 URL 结尾):
- (NSURL *)applicationDocumentsDirectory {
NSString *documentsDirectoryPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
return [NSURL URLWithString:documentsDirectoryPath];
}
我是这样称呼它的:
NSURL *fileURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"AllBooking.json"];
NSLog(@"%@", [fileURL absoluteString]);
日志语句正确打印:
/Users/abhinav/Library/Developer/CoreSimulator/Devices/8C8ACA37-0D0F-4FED-A431-BA35EF9F08F1/data/Containers/Data/Application/20B049C0-9A0D-4826-9867-027607D02715/Documents/AllBooking.json
在我的项目中,我将 jsondata 保存到名为 AllBooking.json 的本地 json 文件中。
代码:
NSURL *fileURL =[[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"AllBooking.json"];
URLByAppendingPathComponent 方法将此文件创建到任何不相关的路径!
和
[自申请文件目录]returns
[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory
inDomains:NSUserDomainMask] lastObject];
有什么方法可以为文件提供自定义路径吗?
嗯,URLByAppendingPathComponent
一切正常。问题出在您的 applicationDocumentsDirectory
方法中。
我刚给你一个运行。这是我的 applicationDocumentsDirectory
方法的样子(假设您想要 return URL 用于文档目录;但是为了清楚起见,方法名称必须以 URL 结尾):
- (NSURL *)applicationDocumentsDirectory {
NSString *documentsDirectoryPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
return [NSURL URLWithString:documentsDirectoryPath];
}
我是这样称呼它的:
NSURL *fileURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"AllBooking.json"];
NSLog(@"%@", [fileURL absoluteString]);
日志语句正确打印:
/Users/abhinav/Library/Developer/CoreSimulator/Devices/8C8ACA37-0D0F-4FED-A431-BA35EF9F08F1/data/Containers/Data/Application/20B049C0-9A0D-4826-9867-027607D02715/Documents/AllBooking.json