I’m looking at my coworker’s C# code (which I’m a noob at) and seeing that he’s transferring me the file size of his JSON object for me to check on the iPad end. He does this by doing:
long fileSize = new System.IO.FileInfo(fileName).Length;
I was wondering what the equivalent way to test for this is in objective-c. I consume the data using NSURLConnection and didn’t know how to check if the file size would be equivalent. I later parse the JSON object, but I want to check if the transfer was successful by the file size.
I’ve tried:
NSString *responseString = [[NSString alloc] initWithData:self.ReceivedData encoding:NSUTF8StringEncoding];
NSLog(@"fileSize: %i, %i", [responseString length], [responseString lengthOfBytesUsingEncoding:NSUTF8StringEncoding]);
But both numbers are wrong. I’m not sure what the right way to do this is.
Since I receive it as NSMutableData, I didn’t see an easy way to check the size. The only thing I could think of was to write the NSData to the temp or documents directory and use NSFileManager’s fileSizeAtPath: method to find the file size. Is there a better way w/o writing the file to one of the available directories? Thanks!
NSMutableDatainherits fromNSData. You can use itslengthmethod to get the number of bytes your data object contains. The corresponding C# method you provide is documented here, which also specifies that the size is in bytes.