在命令行工具app xcode objective c中打开文件

时间:2013-06-03 17:15:31

标签: xcode

我想在objective-C(命令行工具)中读取一个简单的文本文件,这是我的代码

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{

@autoreleasepool {

    // insert code here...
    NSLog(@"Hello, World!");


    NSBundle *bundle = [NSBundle mainBundle];
    NSString *aPath = [bundle pathForResource:@"data" ofType:@"txt"];




    BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:aPath];

    if (fileExists) {
        NSLog(@"THERE!");
    } else {
        NSLog(@"NOT THERE!");
    }

但是我尝试了/Users/Me/dat a或data.txt这样的完整路径到xcode中但它只是不会加载文件,我错过了什么?谢谢!

1 个答案:

答案 0 :(得分:2)

命令行工具没有与之关联的捆绑包。您必须将文件存储在其他位置(例如,/Library/Application Support中的目录,或者如果您要将工具安装到/usr/local/share,则可能位于/usr/local中的某个位置)并从那里。