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.
Related
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
I know this must be very simple. But I can't figure it out.
I have a Help.plist file, it has a key and then a dictionary with string values.
Where is the content for these dictionarys generally held.
When I search the string value in XCODE it does not come up.
This is leading me to believe it in the /documents/ section of the app. Should I use something like text edit to edit the value?
Note : I know the .plist file is a file of XML type. I do not want to edit this. I want to edit the contents of the dictionary.
Thanks
First get the dictionary for editing
NSMutableDictionary* dict = [[NSDictionary dictionaryWithContentsOfURL:[[NSBundle mainBundle] URLForResource:#"Help" withExtension:#"plist"]] mutableCopy];
Then you can edit it with the functions in NSMutableDictionary...
I don't think you can edit the contents of a file in your app's resources, so you may want to save it to the Documents folder after editing...
In my code I have an item that is a list of alternating strings and images that I wish to save. I know how to save individual images in their JPEG representations, but is there a simple and efficient way I don't know of in Objective-C to save all these items (or at least the images since I can handle the strings myself) while maintaining their order?
ex: item 1 ("hi", image, "how", image, "are", image), item2("this", image, "is", image)
maintaining item order is not important to me, but the order in the list is. The first thing that comes to mind is simply to save every single image, and have an algorithm to create then remember their imagename and then save and load the text list. Is there an easier way to do this? Can I literally just save the array itself as a plist and be on my merry way? (i've only seen examples of these with strings)
I believe I understand what you're going for. You'd like to save an array of items, each of which contains an array of Objects that contain an image and a string. If this is accurate then you're idea of saving the array to a plist is correct.
In short you would create NSDictionaries of your Objects and save the array of them. Like so:
In your object you'll have a method similar to this:
- (NSDictionary *)dictionaryOfItemData;
{
NSMutableDictionary * mutableDictionary = [[NSMutableDictionary alloc] init];
[mutableDictionary setObject:imageName
forKey:#"imageName"];
[mutableDictionary setObject:UIImagePNGRepresentation([self image])
forKey:#"imageData"];
return mutableDictionary;
}
Then you would save an array of these (or array of arrays, or whatever your desire based on file size concerns, need to load, etc.) like so in the proper place:
[[arrayOfDictionaries description] writeToFile:path
atomically:YES
encoding:NSUTF8StringEncoding
error:NULL];
Which gives you the plist, that you can load and hydrate by creating your image with data and setting the image name from the dictionary.
~Good Luck
i read this command from cocoadev.com but was not able to get it plz help me in explaining what this line of code do
[array addObject:[NSNumber numberWithInt:15]];
You have to use NSMutableArray instead of NSArray to use the method addObject:.
It adds a number object with the integer value of 15 to the collection (presumably an NSMutableArray) called array.
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.