Getting Error Domain=NSCocoaErrorDomain Code=261 with simple case - ios

I have the following bit of code. path is a bundle path to a file in my bundle.
_dictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
NSError *error;
NSString *string = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
What I don't understand is the _dictionary variable looks fine. It was able to get the contents and convert it to a dictionary. However, the simple case of converting the file to a string is giving me an error:
$2 = 0x12eb3d50 Error Domain=NSCocoaErrorDomain Code=261 "The operation couldn’t be completed. (Cocoa error 261.)" UserInfo=0x12eb3550 {NSFilePath=/Users/ben/Library/Application Support/iPhone Simulator/6.1/Applications/82CBC7A6-2C4C-4330-9B29-7940C961B5B7/FMA.app/Licenses.plist, NSStringEncoding=4}
How can I read the PLIST file as a simple XML string? I've checked the encoding and the file is UTF-8 encoded (as is also indicated by the XML content headers). If I change the encoding to something else, I don't get an error, but the string is garbled! Any ideas?

If Xcode is copying the plist file into your app bundle then you should ensure it is copied as an XML file and not as binary (which seems to be the default setting now).
In Xcode, go to your project, select the target, go to build settings, search for 'property', change the drop-down setting for 'Property List Output Encoding' to XML.

Related

GoogleDataTransport][I-GDTCOR001006] problem?

II had been working on my project like normal when all of a sudden I got this error:
> 2020-05-20 01:42:40.798198-0400 [10404:4763011] [GoogleDataTransport][I-GDTCOR001006] There was an error reading extension bytes from disk: Error Domain=NSCocoaErrorDomain Code=260 "The file “event-16738913419221216480” couldn’t be opened because there is no such file." UserInfo={NSFilePath=/var/mobile/Containers/Data/Application/B3D1204C-A52F-496C-89D8-388D4A75C102/Library/Caches/google-sdks-events/event-16738913419221216480, NSUnderlyingError=0x2838cdbf0 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}
The error occures in the file seen bellow.
The specific line error is:
> Thread 1: EXC_BAD_ACCESS (code=2, address=0x16bcfffb0)
For the second line here:
NSString *pathString = [self
decodePath:[NSString stringWithFormat:#"/%#", originalPathString]]; //this line
FPath *path = [[FPath alloc] initWith:pathString];
FRepoInfo *repoInfo = [[FRepoInfo alloc] initWithHost:host
isSecure:secure
withNamespace:namespace];
FFLog(#"I-RDB095002", #"---> Parsed (%#) to: (%#,%#); ns=(%#); path=(%#)",
url, [repoInfo description], [repoInfo connectionURL],
repoInfo.namespace, [path description]);
FParsedUrl *parsedUrl = [[FParsedUrl alloc] init];
parsedUrl.repoInfo = repoInfo;
parsedUrl.path = path;
return parsedUrl;
I tried removing my pods and re-installing them. This did not change anything.
I have also tried restarting my computer.
How can I solve this problem?
Update:
After further testing, I found that the error occurs in various files within the firebaseDatabase folder, I also was able to isolate it to one query to the database found in multiple files. However under files after un-commenting it, it began working as before again. Given the number of queries however I still have the issue.
Also have an issue active on giithub: https://github.com/firebase/firebase-ios-sdk/issues/5661.
Run pod update
This issue was fixed a few weeks ago. See https://github.com/firebase/firebase-ios-sdk/issues/5421

iOS - Can't Read File Located In Documents Directory

I am trying to read a file in which I successfully downloaded and saved in the Documents directory. However, when I try to read it, if fails.
Here is the error:
2016-03-28 21:00:26.585 App[569:4103] Path is /var/mobile/Applications/3AFA2430-C0DC-44CD-95F8-A89D82B2C348/Documents/combo.bin
2016-03-28 21:00:26.603 App[569:4103] Error in reading Error Domain=NSCocoaErrorDomain Code=257 "The operation couldn’t be completed. (Cocoa error 257.)" UserInfo=0x17df29d0 {NSFilePath=/var/mobile/Applications/3AFA2430-C0DC-44CD-95F8-A89D82B2C348/Documents/combo.bin, NSUnderlyingError=0x17df90b0 "The operation couldn’t be completed. Permission denied"}
Here is my code:
NSError *error;
NSData *firmwareContentData = [NSData dataWithContentsOfFile:FIRMWARE_LOCAL_PATH options:NSDataReadingMappedIfSafe error:&error];
NSLog(#"Path is %#", FIRMWARE_LOCAL_PATH);
if(error)
{
NSLog(#"Error in reading %#", error);
return;
}
local path is
#define FIRMWARE_LOCAL_PATH [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:#"combo.bin"]
I tried using NSTemporaryDirectory(), using NSFileManager to get the contents of the file, but none of them works. Any idea why? Thanks in advance!
You have a permissions error. You don't have the right permissions to open the file. Wherever you got it from, you're locked out. You might try to download it in the simulator, and check it through Apple's file system to see what permissions it actually downloaded with. The path is:
~/Library/Developer/CoreSimulator/Devices//data/Containers/Data/Application//Documents.
Replace the two big random strings with the directories that show a mod date of today, or NSLog the real path from your iOS app.

Strange characters contain when I get JSON data from server in developing iOS App

I am developing iOS App. I get JSON data from PHP server in the following code.
NSURL *requestUrl = [NSURL URLWithString:urlString];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:requestUrl];
NSHTTPURLResponse *httpResponse;
NSData *data = [NSURLConnection sendSynchronousRequest:request
returningResponse:&httpResponse error:nil];
NSString *str= [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSError *e = nil;
NSArray *array =[NSJSONSerialization JSONObjectWithData:[str dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingAllowFragments|NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:&e];
NSLog(#"str=%#",str);
NSLog(#"error=%#",e);
NSLog(#"error=%#",e) says: "Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Badly formed object around character 3010.) UserInfo=0x15ed7f40 {NSDebugDescription=Badly formed object around character 3010.}".
NSLog(#"str=%#",str) contains: "\343\201" unexpectedly.
I think the problem is JSON data is not proper and above "\343\201" causes this problem.
Could you tell me how to solve this problem?
A quick search of the error message indicates that the problem is due to malformed JSON data.
Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x984aeb0 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}
This would imply that you should allow fragments, which I can see is an option that you are already using. I suggest that you try to execute the JSON query in a browser and verify the JSON. You could copy it into a text editor to make sure that it is valid. Sharing that string (if it is not sensitive info) would allow us to further isolate the problem.
Some other people on SO are reporting similar issues, for example here. I suggest that you take the error message text "Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" and search SO for more clues on what caused their problems.
You might also want to try a different tact, where you set the options to nil (for purposes of a test) and then execute again and validate results.
UPDATE
OK, the complete error message is:
NSLog(#"error=%#",e) says "Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Badly formed object around character 3010.) UserInfo=0x15ed7f40 {NSDebugDescription=Badly formed object around character 3010.}".
Based on this you have some bad text in your JSON. Output to a text file and share if at all possible. A search of SO with these key words "JSON Badly formed object around character" reveals four answers of which this is one. Please review to see if those resolve your issue.
"\343\201" is just a Hiragana character. Absolutely normal and no problem from JSON. Post the whole string that you think causes the problem.

How to create a NSString from csv file of around 2mb in size?

I have a csv file of around 2.2 mb data present in the app bundle. I am trying to store the content of this csv file in a NSString variable. The code is used for that is:
NSString *strBundle = [[NSBundle mainBundle] pathForResource:#"large" ofType:#"csv"];
NSError *error = nil;
NSString *dataStr = [NSString stringWithContentsOfFile:strBundle
encoding:NSUTF8StringEncoding
error:&error];
However, the above code deosn't store the text of the file in dataStr variable. Instead, I get the following error message:
Error=Error Domain=NSCocoaErrorDomain Code=261 "The operation couldn’t be completed. (Cocoa error 261.)" UserInfo=0x1fdab580 {NSFilePath=/var/mobile/Applications/0C68E393-FBBE-45F6-819E-336D31C78043/DemoApp.app/large.csv, NSStringEncoding=4}
If, I delete content of the file such that the file is relative smaller than 2.2 mb, the above code works fine and get the string value in dataStr variable. Can some one tell me please why is this happening. Is there any limit on the amount of data that can be stored in NSString variable. How do I store the data of CSV file then?
NSCocoaErrorDomain Code=261 is: NSFileReadInapplicableStringEncodingError.
Read error because the string encoding was not applicable.
Access the bad encoding from the userInfo dictionary using the NSStringEncodingErrorKey key.
That would indicate the file is not NSUTF8StringEncoding.
You can use:
- (id)initWithContentsOfFile:(NSString *)path usedEncoding:(NSStringEncoding *)enc error:(NSError **)error
and it will return the encoding or an error.
An NSString object initialized by reading data from the file named by
path. The returned object may be different from the original receiver.
If the file can’t be opened or there is an encoding error, returns
nil.
Apple Docs

Xcode Unit Test and Writing Data to Disk

I am trying to write logic test to debug a a method that saves image data to disk. I use the following lines to write image data to the documents directory:
docspath = [[NSString alloc] initWithString:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, TRUE) objectAtIndex:0]];
fullpath = [[NSString alloc] initWithFormat:#"%#/%#", docspath, _name];
BOOL written = [fileData writeToFile:fullpath options:NSDataWritingAtomic error:&dataerror];
Except, the data is not written and the error description returns no such file or directory. I am running this code under a test target in the iOS 5.1 simulator. Do I need to create intermediate directories to get to the documents directory, or is there another issue with writing to disk under a test target?
The error returned:
2012-06-29 16:13:31.819 otest[4597:7b03] /Users/myaccount/xcode_projects/myproject/myproject/myfolder/MMFileManager.m - 285: Error Domain=NSCocoaErrorDomain Code=4 "The operation couldn’t be completed. (Cocoa error 4.)" UserInfo=0x1e1e3a0 {NSUserStringVariant=Folder, NSFilePath=/Users/myaccount/Library/Application Support/iPhone Simulator/5.1/Documents/mmLogo.png, NSUnderlyingError=0x1e1b430 "The operation couldn’t be completed. No such file or directory"}
Unit test is not in your app, therefore, you can't access Documents directory in the unit test.
refer to NSHomeDirectory in iPhone unit test

Resources