Why does NSMutableDictionary writeToFile return no? - objective-c

I have the following, where path is the documents folder. myArray is an NSMutableDictionary. I'm running this in the simulator.
BOOL success = [self.myArray writeToFile:path atomically:YES];
The above always returns no. I can see in the target folder that nothing was written.
The path looks like this:
/Users/username/Library/Application Support/iPhone Simulator/7.1/Applications/79D8982F-9995-4831-83B9-E2749F2261CD/Documents/
Any ideas what I'm doing wrong?

I expect one or more objects within the dictionary cannot be written:
Discussion
This method recursively validates that all the contained
objects are property list objects (instances of NSData, NSDate,
NSNumber, NSString, NSArray, or NSDictionary) before writing out the
file, and returns NO if all the objects are not property list objects,
since the resultant file would not be a valid property list.
Where Property List Objects are (see here):
NSArray, NSDictionary, NSString, NSData, NSDate and NSNumber.

The path points to the Documents directory but not to any particular file in that directory. You can't write a file to a directory, you have to give it a file name. The other obvious possibility would be that self.myArray == nil. Plus what Droppy said is absolutely correct.

/Users/username/Library/Application Support/iPhone Simulator/7.1/Applications/79D8982F-9995-4831-83B9-E2749F2261CD/Documents/
The path just represents a navigation to Documents Directory but not pointing to some kind of file.
Add some file name with the below line and add dictionary to the path
NSArray *sandboxArray=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path= [[arr objectAtIndex:0]stringByAppendingPathComponent:#"login.plist"];
write something similar to the path

Related

How to take parameters from plist?

I am quite new at Objective-C programming, I was asked to develop a framework that could be implemented in IOS apps. This framework has three methods (that take a model object as an argument) that perform API comsumption and return a message (that takes from response). The problem is that I was asked to store the module parameters in plist, and I don´t have a good clue what this means. I been reading about plist and I know they can store serialized objects. But I really don´t understand what it means to be storing all parameters on this file.
A plist is essentially a dictionary (or NSDictionary) -- with keys and values -- written to a specific file format that iOS expects.
To write a plist file is easy when you do it from Xcode. In Xcode 10.3 you can go to "File" -> "New" --> "File..." and select "Property List" from the types of files you see:
I created a file (as an example) named "SomeFile.plist" and then added a couple keys & values to it:
Now after you get this file included in your new project, you need to read the keys & values back in. Here is a related question that shows you different ways to read the plist / dictionary, such as:
NSString *path = [[NSBundle mainBundle] pathForResource: #"YourPLIST" ofType: #"plist"];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile: path];
NSString *name = [dict stringForKey: #"RaphaelName"];

How can I delete file extensions of all files in an array?

When the mac osx app runs, it will create an array that contains the NSString names and extensions for all the files in a directory. However, I want to delete the file extensions from each file in the array before I display them to the user and place these names in an array. How can I accomplish this?
ex. picture.jpeg, image.jpeg, and picture2.png to picture, image, and picture2
You can use stringByDeletingPathExtension and KVC collection operators:
NSArray *original = #[#"picture.jpeg", #"image.jpeg", #"picture2.png"];
NSArray *modified = [original valueForKeyPath:#"stringByDeletingPathExtension"];
Here valueForKeyPath returns an array containing the result of calling stringByDeletingPathExtension on all objects in the array.
NSString's – stringByDeletingPathExtension should do the trick for you.
-[NSString stringByDeletingPathExtension] is what you're looking for.

List all files in Documents Folder

I need to list all files in Documents folder and get their names...because I don't know the name of the files..then I can't use:
NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString* foofile = [documentsPath stringByAppendingPathComponent:#"filename" ];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:foofile];
How can I do?
Resuming: I need the name of the .png's files in Documents folder
NSFileManager Class Reference
contentsOfDirectoryAtPath:error: Performs a shallow search of the
specified directory and returns the paths of any contained items.
(NSArray *)contentsOfDirectoryAtPath:(NSString )path error:(NSError *)error Parameters path The path to the directory whose contents you want to enumerate. error On input, a pointer to an error object. If an
error occurs, this pointer is set to an actual error object containing
the error information. You may specify nil for this parameter if you
do not want the error information. Return Value An array of NSString
objects, each of which identifies a file, directory, or symbolic link
contained in path. Returns an empty array if the directory exists but
has no contents. If an error occurs, this method returns nil and
assigns an appropriate error object to the error parameter
Discussion This method performs a shallow search of the directory and
therefore does not traverse symbolic links or return the contents of
any subdirectories. This method also does not return URLs for the
current directory (“.”), parent directory (“..”), or resource forks
(files that begin with “._”) but it does return other hidden files
(files that begin with a period character). If you need to perform a
deep enumeration, use the
enumeratorAtURL:includingPropertiesForKeys:options:errorHandler:
method instead.
The order of the files in the returned array is undefined.
Availability Available in iOS 2.0 and later.
That should fulfill your needs

.plist file writing failed! what is wrong with this code?

Nothing is written in my plist file after this code. What is wrong with my code?
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:#"aFile.plist"];
NSMutableDictionary *reqData = [NSMutableDictionary dictionaryWithContentsOfFile:finalPath];
/*
some modifications to "reqData"
*/
[reqData writeToFile:finalPath atomically:YES];
Nothing is written in file. what could be the problem?
You'd better write to Document folder:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES);
NSString *path = [paths objectAtIndex:0];
Update
According to NSDictionary Class Reference:
This method recursively validates that all the contained objects are property list objects (instances of NSData, NSDate, NSNumber, NSString, NSArray, or NSDictionary) before writing out the file, and returns NO if all the objects are not property list objects, since the resultant file would not be a valid property list.
Is there any objects with a type other than these valid ones in your whole dictionary?
You are trying to write the file back to the app bundle.
That's what's most likely causing the error.
What if you try writing it somewhere else (e.g. in your desktop folder)?
If you're including the plist with your application, you'll want to copy that file into the Documents directly when the app first starts up (if it hasn't already been copied there). Then, any read and write operations you want to do on the plist should be done from the copy in the Documents directory instead of the version in the app bundle.
First Thing to remember:
You can read a plist file from resources but you can't modify it.
if you want to modify,
copy that file to Documents directory
copy contents of plist into array or dictionary depending on its type
make changes you want
Finally.... save it back to documents directory
that do the trick

Open .string files as an NSDictionary

How do I open a .string file as an NSDictionary?
Edit: I want to be able to use it like this:
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:#"dict.plist"];
NSDictionary *strings = [[NSDictionary alloc] initWithContentsOfFile:#"Strings.strings"];
If you really want it in a dictionary, you can load it using [NSDictionary dictionaryWithContentsOfFile:], since it is in “Old-style ASCII” format. I have used this technique before on Mac OS X, but I'm not sure you can do the same for iOS.
However, if you want a translation for a particular string, there are at least two ways to do it:
NSLocalizedStringFromTable() will allow you to load strings from files other than the normal Localizable.strings file. Provide the name of your strings file (without the extension).
NSBundle's localizedStringForKey:value:table: method. This essentially performs the same operations as the method above, and as above, provide the name of your strings file without the extension.
.string files just store key/value pairs, like:
"StringKey" = "some localized text";
you can get the text for a specific key using NSLocalizedString. If you want to get all the strings in a file, I suppose you could read the Localizable.strings file and parse it.