作者darktt (小朱)
標題Re: [問題] NSData writeToFile cocoa error 4
時間Tue Sep 2 17:14:56 2014
※ 引述《issuemylove (skill)》之銘言:
: 想請問各位大大
: 關於NSData的writeToFile正確用法。
: 我寫了如下程式碼來將我下載的圖片存入到ipad中:
: 如圖 http://ppt.cc/UJAP
: 但是一直發生 Cocoa error 4的錯誤,請問我是哪邊有弄錯嗎???
: log: http://ppt.cc/9Zsw
: 謝謝大大們的幫助!
我用了點我的方法重新驗證原 PO 的問題,Code 如下:
NSString *saveDirectory = [[DTFileController mainController]
documentPathWithFileName:@"/my_images"];
[[DTFileController mainController] createDirectoryAtPath:saveDirectory];
NSString *savePath = [saveDirectory stringByAppendingPathComponent:@"image.jpg"];
NSData *imageData = [NSData dataWithContentsOfURL:
[NSURL URLWithString:
@"
https://www.google.com.tw/images/srpr/logo11w.png"]];
NSError *error = nil;
[imageData writeToFile:savePath options:NSDataWritingAtomic error:&error];
if (error != nil) {
NSLog(@"%@", error);
}
我是沒發生原 PO 的問題,
我的建立資料夾的參數 withIntermediateDirectories: 是 NO,
不過我用了 Yes 也沒問題,
所以還是不知道問題在哪裡,
不過原 PO 可以嘗試看看我寫的 DTFileController?
https://github.com/Darktt/DTFileController
--
→ lwecloud: 為什麼手把是限制級!?
推 kris4588: 因為會震動啊~
推 awano14: 3樓神回
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 122.117.153.197
※ 文章網址: http://www.ptt.cc/bbs/MacDev/M.1409649303.A.28A.html
推 redok: 我研究後再回覆QQ,不過目前感覺怪怪的就是了,若再奇怪會 09/02 20:13
→ redok: 丟專案上來,謝謝大大們! 09/02 20:13
推 issuemylove: 我試了大大的library情況也是Cocoa Error 4 09/03 11:57
你的完整的 error 資料是:
The operation couldn’t be completed. No such file or directory
我自己是不會只有 log error 的 localizedDescription
因為他的描述太片面了,log 完整的 error 的資料才能知道完整的原因。
※ 編輯: darktt (122.117.153.197), 09/03/2014 13:11:16
推 issuemylove: 謝謝大大的幫助!上一篇已經找出我的問題了,我目錄 09/03 14:25
→ issuemylove: 沒有建立成功 (參數帶錯)orz 謝謝! 09/03 14:25