NSString's initWithContentsOfFile is failing to load any file bigger than 40kb, why and what is the solution?

2.5k views Asked by At

I want to load a file into an NSString variable and am using NSString's initWithContentsOfFile to do so. However any file that is larger than 40kb the method fails and nil is returned to my variable. Here is the code I am using:

NSString *fileContents;    
fileContents = [[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];

I'm guessing it is failing due to memory shortage but surely it can handle a file as small as 40kb?!

1

There are 1 answers

0
jamesdeacon On

After printing the error and discovering I was receiving Cocoa error code 261 I was quickly able to discover with a quick Google that my problem was that it was the NSStringEncoding argument that was causing a problem. I changed from NSUTF8StringEncoding to NSASCIIStringEncoding and my problem is fixed.