Подтвердить что ты не робот

Получить путь к файлу и 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.

Итак, я немного смущен, кто-то может мне помочь?

Спасибо.

4b9b3361

Ответ 1

NSTemporaryDirectory() предоставляет полный путь к временному каталогу. Вместо того, чтобы использовать mainBundle, вы пробовали

NSString *filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"temp.pdf"];

Если вам нужен URL, сделайте следующее:

NSURL *furl = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:@"temp.pdf"]];

Ответ 2

Swift 2.0

let tmpDirURL = NSURL.fileURLWithPath(NSTemporaryDirectory(),isDirectory: true)
let fileURL = tmpDirURL.URLByAppendingPathComponent("stuff").URLByAppendingPathExtension("gif")
print("FilePath: \(fileURL.path)")

Ответ 3

Для Swift 3.0

var fileUrl: URL = URL(fileURLWithPath: NSTemporaryDirectory())
fileUrl.appendPathComponent("foo")
fileUrl.appendPathExtension("bar")