1

I'm pulling a .json file from my server and trying to read it into an NSDictionary in my app. Some of the file is being correctly parsed but it is incomplete - only some of the file is being read.

The strange thing is that the NSData is an equal length to the actual file, so it seems that it has full access, at least at some stage. When I log the NSData however, it seems to be way too short for the size of the file.

Here's the code I'm using to find the bug:

//SYNC BOOL
if (isSyncing){ return; }
isSyncing = true;

//FETCH BOOTSTRAP
NSError * fetchError = nil;
NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:syncPath]
                                      options:kNilOptions
                                        error:&fetchError];
if (fetchError){   [self error]; return; }

//PARSE JSON
NSError * jsonError = nil;
NSDictionary * json = [NSJSONSerialization JSONObjectWithData:data
                                                      options:kNilOptions
                                                        error:&jsonError];
if (jsonError){ [self error]; return; }


NSLog(@"data length is %lu", data.length);
NSLog(@"json is %@", json);

I've tried loading remotely from the server and locally from NSBundle - same result.

Could this be related to encoding / a rogue character in the JSON / some NSData max length?

Those options on the NSData fetch method and JSON Serialisation method, I've always left blank with no issue in the past, in terms of what's being pulled it's the same. I've also tried requests and sessions etc with no love.

EDIT: I should add that when I log the the .allKeys of the json dictionary, it returns all keys correctly (including those not included in the log of the dictionary itself). This coupled with the correct NSData length implies that the data is in fact there, in completion. An explanation would be if the NSLog itself is somehow being truncated, implying an error when none exists. The problem is I haven't changed anything there. It could be a beta bug in the new Xcode.

EDIT B: Logger Error on Xcode 9?

NSString * string = @"";
for (int n = 0; n < 10000; n++){
    string = [NSString stringWithFormat:@"%@ %i",string,n];
}
NSLog(@"string is %@", string);

Outputs to 6.7k not 10k.

Johnny Rockex
  • 4,136
  • 3
  • 35
  • 55

2 Answers2

1

See if you get different results with:

NSLog(@"json is %@", json.description);

Or to rule out NSLog altogether, maybe breakpoint the code there, and right click on json in the variables pane of the debug area and choose Print description of "json"

My experience is NSJSONSerialization will return an error for malformed JSON.

CodeWriter23
  • 317
  • 3
  • 10
  • 1
    .description returns equally truncated string. The breakpoint approach returns the full data on investigation, but both NSData and NSLog return truncated versions. Very strange. – Johnny Rockex Oct 25 '17 at 19:55
  • 1
    One of my other tricks: NSString *jsonString = [[NSString alloc] initWithData: json encoding: NSUTF8StringEncoding]; NSLog(@"json=%@", jsonString); – CodeWriter23 Oct 25 '17 at 19:58
  • it seems the truncation point has shifted, but it still not complete. – Johnny Rockex Oct 25 '17 at 20:02
  • Looks like you're going to have to set up WireShark and see what's coming over the wire. Or maybe CharlesProxy (haven't used that myself yet, but apparently it gives you a way to trace connections with https:// servers you don't control. – CodeWriter23 Oct 25 '17 at 20:04
  • I've loaded from a local file also, sadly with the same result. I think the file, and the connection to the file on the server is valid. – Johnny Rockex Oct 25 '17 at 20:06
  • 1
    My mistake. I'm sorry, you did mention trying it with a file in your initial report. And thanks for the answer below about truncated NSLog. BTW thanks for the upvote, it got me to "Comment Everywhere" status. – CodeWriter23 Oct 25 '17 at 20:32
1

This is some silly feature in Xcode itself it seems. This define works for the full NSLog:

#define NSLog(FORMAT, ...) printf("%s\n", [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);

Taken from here:

NSLog on devices in iOS 10 / Xcode 8 seems to truncate? Why?

How many developers will spend time looking for imaginary bugs in the lazy logger...

For those coming afterwards, I'm running Xcode 9.0.

Johnny Rockex
  • 4,136
  • 3
  • 35
  • 55