2015-10-13 32 views
1

在我的項目中,我將jsondata保存到名爲AllBooking.json的本地jsonfile中。如何在iOS中的URLByAppendingPathComponent中添加自定義路徑

代碼:

NSURL *fileURL =[[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"AllBooking.json"]; 

URLByAppendingPathComponent方法創建該文件的任何無關緊要的道路!

而且
[自applicationDocumentsDirectory] ​​回報

[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory 
inDomains:NSUserDomainMask] lastObject]; 

有什麼辦法,我可以給自定義的文件路徑?

+0

同時添加的代碼[自applicationDocumentsDirectory] ​​ –

+0

你被無關的路徑是什麼意思? –

+0

@ Er.ShreyanshShah我已經添加了[self applicationDocumentsDirectory] ​​ – Bindi

回答

1

那麼,URLByAppendingPathComponent工作都很好。問題將出現在您的applicationDocumentsDirectory方法中。

我剛給你跑步。這裏是我applicationDocumentsDirectory方法看起來像(假設你想返回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 
+0

謝謝@Abhinav它幫助!得到了錯誤和解決方案。打印相同的日誌。 – Bindi

+0

沒問題。很高興幫助!你能接受這個答案是爲了他人的利益嗎?@ Bindi – Abhinav

相關問題