Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

stringWithContentsOfFile and initWithContentsOfFile return null after several runs

Tags:

ios

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

For the 1st three consecutive runs of thee app (Restarting a new session without exiting), the data is read in fine. However on the fourth attempt, the data returned from the file is all nulls.

I've verified the file integrity. The issue exists when using stringWithContentsOfFile or initWithContentsOfFile.

After many hours of troubleshooting, I believe the issue is somehow related to a buffer being cleared within the above mentioned methods.

Any insight regarding this issue is greatly appreciated. I've tried many things with no luck.

Here's the code I use to read in the file:

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

Here's the code I use to display certain contents of the file (The contents are 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 invocation of the code above, the data returned is blank and NSLOG is returning nulls

Thanks so much for any help!

like image 770
Native_Mobile_Arch_Dev Avatar asked Oct 28 '10 15:10

Native_Mobile_Arch_Dev


1 Answers

Maybe I'm a little bit late with answer, but, anyway, maybe somebody will find it useful. OK, I have also spent a day trying to figure out why my custom class for scrollable view is working 3 times and refuse at the 4-th time... I found that the problem has quite the same attributes as yours: nested NSString objects unexpectedly disappear. Though pointers point to the same address in memory, memory is already filled with quite arbitrary objects instead my NSStrings. And I paid attention that I created these NSStrings using the following class method:

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

So, I'm not the owner of these NSStrings. And I assumed that to be the owner can be a solution, so I created my NSStrings through alloc and

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

instance method.

App was repaired!

like image 97
Ilya Avatar answered Sep 25 '22 23:09

Ilya