[問題] 加入檔案的問題

看板MacDev作者 (盧小小)時間10年前 (2014/01/29 16:01), 編輯推噓0(007)
留言7則, 2人參與, 最新討論串1/1
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)

01/29 16:31, , 1F
Command Line Tool的產出記得是沒有封裝, 就單純的binary
01/29 16:31, 1F

01/29 16:32, , 2F
在這種情況下自然也沒有Resource管理的地方以及需要
01/29 16:32, 2F

01/29 16:33, , 3F
而你所使用的讀檔方式,是直接到Package中的Resource裡去找
01/29 16:33, 3F

01/29 16:33, , 4F
當然會找不到
01/29 16:33, 4F

01/29 16:35, , 5F
試著開一個Cocoa Application的專案, 再試一次從Resouce中
01/29 16:35, 5F

01/29 16:35, , 6F
讀檔的動作吧, 應該是會成功的.
01/29 16:35, 6F

01/30 21:47, , 7F
感謝前輩解答
01/30 21:47, 7F
文章代碼(AID): #1IwBLpSH (MacDev)