Получить путь к файлу и URL-адрес для файла в каталоге temp
Я пытаюсь получить путь к файлу с именем " temp.pdf", который находится в папке NSTemporaryDirectory (я проверил, файл существует).
Я использую этот :
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"temp" ofType:@"pdf" inDirectory:NSTemporaryDirectory()];
Я пробовал с :
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"temp.pdf" ofType:@"" inDirectory:NSTemporaryDirectory()];
и :
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"temp" ofType:@"pdf"];
но кажется, что это не работает, возвращаемое значение всегда равно null.
Итак, я немного запутался, кто-то может мне помочь ?
спасибо.
3 ответов:
NSTemporaryDirectory()предоставляет полный путь к временному каталогу. ВместоmainBundleты пробовалNSString *filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"temp.pdf"];если вам нужен URL вместо этого, сделайте это:
NSURL *furl = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:@"temp.pdf"]];
Swift 2.0
let tmpDirURL = NSURL.fileURLWithPath(NSTemporaryDirectory(),isDirectory: true) let fileURL = tmpDirURL.URLByAppendingPathComponent("stuff").URLByAppendingPathExtension("gif") print("FilePath: \(fileURL.path)")
Для Swift 3.0
var fileUrl: URL = URL(fileURLWithPath: NSTemporaryDirectory()) fileUrl.appendPathComponent("foo") fileUrl.appendPathExtension("bar")
Comments