views:

135

answers:

1

In this program when I debug, it is showing the nil for fileNameString. I could not understand what is the problem. Please help me ?

@implementation fromFileRead1
NSString *fileNameString;

-(id)init
{
    if( (self = [super init]) ) 
    {
        fileNameString = [[NSString alloc] initWithContentsOfFile: @"enemyDetails.rtf" encoding:NSUTF8StringEncoding error:nil];
        NSArray *lines = [fileNameString componentsSeparatedByString:@"\n"]; 
        for (id *line in lines)
        {
            NSLog(@"Line1%@", line );
        }
    }
    return self;
}
@end

Thank You.


-(id)init
{
    if( (self = [super init]) ) 
    {
        NSString *path = @"/Users/sridhar/Desktop/Projects/exampleOnFile2/enemyDetals.txt";
        NSString *contentsOfFile = [[NSString alloc] initWithContentsOfFile:path];
        NSArray *lines = [contentsOfFile componentsSeparatedByString:@"\n"]; 
        for (id *line in lines)
        {
            NSLog(@"Line1%@", line );
        }
    }
    return self;
}
@end
+1  A: 

You need to specify the full path to the file with string so the problem must be with this part:

... initWithContentsOfFile: @"enemyDetails.rtf" ...

What exactly path you need depends on where you store your text file. If it is a resource in your bundle you can get the path using -pathForResource: function from NSBundle:

NSString *fPath = [[NSBundle mainBundle] pathForResource:@"enemyDetails" ofType:@"rtf"];

If it is stored in another directory in your applications sandbox - see Getting Paths to Application Directories section in "Developer Programming Guide" about how to get paths for them.

Vladimir
HiThank You, I thought the full path is not needed. As, it worked for my .png files I thought it could work for this also.
srikanth rongali