作者ishuen (盧小小)
看板MacDev
標題[問題] 加入檔案的問題
時間Wed Jan 29 16:01:53 2014
Programming in Objective-C, 5th, page 405
書上說把TXT檔拉到Xcode的左邊來加入檔案
在跳出的畫面中選複製檔案到專案資料夾
然後用下面的程式碼顯示TXT內容
NSString *txtFilePath = [[NSBundle mainBundle]
pathForResource: @"instructions"
ofType: @"txt"];
NSString *instructions = [NSString
stringWithCotentsOfFile: txtFilePath
encoding: NSUTF8Encoding
error: NULL];
我發現檔案有被加到放.xcodeproj的那個資料夾
但是txtFilePath和instructions跑出來都是(null)
我把檔案加到mainBundle顯示的那個位置就可以
/Users/Jason/Library/Developer/Xcode/DerivedData/
xxxxxxxxx/Build/Products/Debug/instructions.txt
是因為我的專案是Command Line Tool的關係嗎?
我上網查好像是要複製到Resource資料夾
不過Command Line Tool好像沒這個東西
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 211.76.64.223
※ 編輯: ishuen 來自: 211.76.64.223 (01/29 16:05)
→ atst2:Command Line Tool的產出記得是沒有封裝, 就單純的binary 01/29 16:31
→ atst2:在這種情況下自然也沒有Resource管理的地方以及需要 01/29 16:32
→ atst2:而你所使用的讀檔方式,是直接到Package中的Resource裡去找 01/29 16:33
→ atst2:當然會找不到 01/29 16:33
→ atst2:試著開一個Cocoa Application的專案, 再試一次從Resouce中 01/29 16:35
→ atst2:讀檔的動作吧, 應該是會成功的. 01/29 16:35
→ ishuen:感謝前輩解答 01/30 21:47