Question

Well this is embarrassing and I saw a lot of questions answered, and I copied a lot of answers but no one works for me, I'm new in objective C and I know the rules here, I googled it, I saw a lot a pages of objective C, Xcode, Programming, C, Forums, blogs, and here, I'm getting Nuts. Here is my think

The code:

#import <Foundation/Foundation.h>

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

    @autoreleasepool {
        NSError *error = NULL;
        NSString* pathFile = [[NSBundle mainBundle] pathForResource:@"file"
                                                                  ofType:@"txt"];
        NSString* content = [NSString stringWithContentsOfFile:pathFile
                                                            encoding:NSUTF8StringEncoding
                                                               error:&error];
        if (error)
            NSLog(@"Error reading file: %@", error.localizedDescription);
        NSLog(@"%@", content);
    }
    return 0;
}

Its very simple, in the original app it is complex but this is the part where Im getting troubles.

And heres the files in the buble...

enter image description here

Heres is the result:

2014-04-28 21:45:17.213 LeerFile[6930:303] Error reading file: The file name is invalid.
2014-04-28 21:45:17.214 LeerFile[6930:303] (null)

And finally here is the Build phases...

enter image description here

Guys, please, help me with this, I try it but I don't know what Im doing wrong or how to fix it...

Was it helpful?

Solution

The issue is that you're copying the file into /usr/share/man/man1/, but you're looking for the file in your bundle's resources folder. You need to copy the file into the resources folder, or pass a different path to [NSString stringWithContentsOfFile:encoding:error:];.

Also you've checked "Copy only when installing," meaning that it won't copy the files in your debug build.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top