StringWithContentsOfFile and initWithContentsOfFile return null after several starts

I am creating an iOS application that reads in a text file and displays the contents in a UIText field.

For the first three consecutive runs of your application (restarting a new session without exiting), the data is considered a fine. However, in the fourth attempt, the data returned from the file are zeros.

I checked file integrity. The problem occurs when using stringWithContentsOfFile or initWithContentsOfFile.

After many hours of troubleshooting, I believe the problem is that the buffer is cleared as part of the above methods.

Any understanding of this problem is greatly appreciated. I tried a lot of things with no luck.

Here is the code that I use to read in a file:

TheString = [NSString stringWithContentsOfFile:[[NSBundle mainBundle]     
pathForResource:@"My_TextFile" ofType:@"txt"] encoding:NSUTF8StringEncoding error:NULL];

Here is the code that I use to display the specific contents of the file (the content is placed in an array of type NSArray):

NSArray *My_Array;

My_Array= [TheString componentsSeparatedByString:@"\n"];

/* Obtain specific data to display */
    DisplayedData = [My_Array objectAtIndex:M[l]-1];
:
:
/* Display the data in the view */
    MyUITextView.text = DisplayedData;

/* Log the data */
    NSLog(@"%@", MyUITextView.text);

On the 4th call of the code above, the returned data is empty and NSLOG returns nulls

Thanks so much for any help!

+5
source share
1 answer

, , , , , - . , , , 3 4- ... , , : NSString . , my NSString s. , NSString, :

+ (id)stringWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc  error:(NSError  **)error

, NSString s. , , NSString alloc

- (id)initWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc  error:(NSError  **)error 

.

!

+5

All Articles