作者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)
1F:→ atst2:Command Line Tool的产出记得是没有封装, 就单纯的binary 01/29 16:31
2F:→ atst2:在这种情况下自然也没有Resource管理的地方以及需要 01/29 16:32
3F:→ atst2:而你所使用的读档方式,是直接到Package中的Resource里去找 01/29 16:33
4F:→ atst2:当然会找不到 01/29 16:33
5F:→ atst2:试着开一个Cocoa Application的专案, 再试一次从Resouce中 01/29 16:35
6F:→ atst2:读档的动作吧, 应该是会成功的. 01/29 16:35
7F:→ ishuen:感谢前辈解答 01/30 21:47