2011-08-16 16 views
49

NSTemporaryDirectory klasöründe bulunan "temp.pdf" adlı bir dosyanın dosya yolunu bulmaya çalışıyorum (dosya kontrol ettim).Dosya yolu ve dosya için URL'yi temp dizinine getirin

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"temp.pdf" ofType:@"" inDirectory:NSTemporaryDirectory()]; 

Ve: Birlikte denedim

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"temp" ofType:@"pdf" inDirectory:NSTemporaryDirectory()]; 

:

bunu kullanmaktan

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"temp" ofType:@"pdf"]; 

Ama bu işe yaramazsa gibi görünüyor, dönüş değeri her zaman boş.

Biraz kafam karıştı, birisi bana yardım edebilir mi?

Teşekkür ederiz.

cevap

120

NSTemporaryDirectory(), geçici dizine tam bir yol sağlar.

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

Tamam, bunu yaptığımda, [UIDocumentInteractionController interactionControllerWithURL: dosya yolu] ile dosya yolunu kullanmak istiyorum; [NSURL urlWithString: filepath] yaptım, ancak Xcode diyor ki: "Geçersiz şema (null). Sadece dosya düzeni destekleniyor." – HasgardLucian

+19

Deneme NS NSURL * furl = [NSURL fileURLWithPath: [NSTemporaryDirectory() stringByAppendingPathComponent: @ "temp.pdf"]] '' – Joe

+0

Tüm pdf dosyalarını geçici dizinde döndürmek istiyorsak ne olur? –

13

Swift 2,0

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

Swift 3.0 için: kullanmak yerine senin mainBundle bunun yerine bir URL gerekiyorsa, bunu

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

denedi

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