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...
Related
I am using the NSPasteboard to read data off it and perform actions with the received data.
Specifically, I am interested in information of the types listed below in order of priority.
File path
Text data
Image data (this is not copying the image file, but actual parts of the image, for e.g. when you open an image in an editor, make a selection and press command+v)
This is what I have in code
_pasteBoard = [NSPasteboard generalPasteboard];
NSArray *types = [_pasteBoard types];
NSArray *files = [_pasteBoard propertyListForType: NSFilenamesPboardType];
NSString *text = [_pasteBoard propertyListForType: NSStringPboardType];
NSString *text2 = [_pasteBoard propertyListForType: NSPasteboardTypeString];
NSString *rtfText = [_pasteBoard propertyListForType: NSRTFPboardType];
NSData *imageData = [_pasteBoard dataForType: NSTIFFPboardType];
Right now, I am simply testing if I am able to get the required data or not. So When I select some files on the Destkop then files contains the selected file URLs. So that works.
Similarly when I select some parts of an image and copy it to the clipboard then imageData contains some data, which I then write to another file and can see that only the selected potion was copied, so that is OK too. Also, I understand that when I copy an image "file" to the clipboard then too imageData will not be nil, but that is ok because topmost priority goes to file URL and that is the case that will do what needs to be done.
The problem that I have run into is with the lines related to reading text from the pasteboard.
NSString *text = [_pasteBoard propertyListForType: NSStringPboardType];
NSString *text2 = [_pasteBoard propertyListForType: NSPasteboardTypeString];
NSString *rtfText = [_pasteBoard propertyListForType: NSRTFPboardType];
The Problem Descriptio:
I copied the URL of this page and both "text" and "text2" contain the string from the pasteboard. So it good here :)
I copy some lines from a .cs code file opened in TextEdit, the clipboard shows that I have "public.utf8-plain-text" and "NSStringPboardType" but "text" and "text2" are both nil;
From the same C#.NET .cs file, I select a single word, without any breaks for eg. "System.Threading", the clipboard shows that I have "public.utf8-plain-text" and "NSStringPboardType" and both "text" and "text2" have the copied text but any thing copied with spaces or lines will not. "myProject.Core.Helpers" will work but "namespace myProject.Core.Helpers" wont.
Similarly, I copy some text from a RTF file, the clipboard shows that I have a lot of RTF related information along with the same old "public.utf8-plain-text" and "NSStringPboardType" types too, but again "text", "text2" and, this time, "rtfText" are all nil.
So, how do I get the text that I need in all cases from the clipboard? I know that RTF data can contain a lot more then text but I am just concerned with getting the text out in a string file.
Any help will be greatly appreciated.
Thanks!
The problem is almost certainly that you're not accounting for multiple items on the pasteboard. In the old days (OS X 10.5 and earlier), there was only one item on the pasteboard. That item could have multiple types, which were just supposed to be representations of the same thing.
Now, the pasteboard can have multiple items. Each item can still have multiple types. The -types method returns a union of the types of all of the items. But -propertyListForType: will only examine the first item.
You could iterate over the array returned by -pasteboardItems and ask each for its types and then get the property list object for each type.
Or you could, for each type you're interested in, invoke -readObjectsForClasses:options: with a single-element array of the corresponding class. That wouldn't (and you generally shouldn't) distinguish among different plain text types. All of those would match NSString.
The most common approach, though, is to construct an array of all classes you can handle, in order of your app's preference (usually most expressive to least), and make a single call to -readObjectsForClasses:options:. Then process all objects returned. Each object will be for a single item on the pasteboard in the "best" available form.
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.
I am sorry to ask this but I have searched for hours on doing this but I really don't understand it. Please help me. I have a .plist file in my Xcode project and it's root is a Dictionary type. It contains about 50 more dictionaries. Inside the dictionary contains strings. (Dictionary(root) > Dictionary > String. I added a search field to my toolbar and linked it to my code. I am able to get what the user types but then how do I "search" after getting what the user typed? Is there a method for this and how do I link it into my .plist? Thank you so much!!!
You want to search for the user entered string in your Dictionary of Dictionaries?
You're going to have to iterate each dictionary, asking [dict objectForKey:userEntry] in each. Not sure if you want to only find first match or all matches too.
Additionally, you may want to create an abstraction of your Dictionary of Dictionaries to reduce the scale of the problem and clarify the API. In simpler terms, wrap your Dictionary of Dictionaries in a class and put a sensible (non-dictionary-based) set of methods on it. It's probably worth the effort.
To load the plist into a Dictionary, look at [Dictionary dictionaryWithContentsOfFile].
Edit: Filtering options on NSDictionary
Have you looked at the following options for filtering values in an NSDictionary:
[NSDictionary keysOfEntriesPassingTest:] (10.6 and later) or
take the [rootDictionary allValues] NSArray and use Predicates, perhaps like this.
Is there a way to make the localization or Localizable.strings read from directories outside NSBundle?
I am trying to make my app read localizations from a file that is downloaded via a server, is there a way good way to do this?
Thanks in advance.
You will have to write your own MyLocalizedString function which reads the file manually. A .strings file is actually an old type of property list, so it can be read using the NSPropertyListSerialization class like this:
id plist = [NSPropertyListSerialization
propertyListWithData:[NSData dataWithContentsOfFile:stringsFilePath]
options:0
format:NULL
error:&error];
plist is just an NSDictionary, so you can read a string value from the result like this:
[plist objectForKey:#"my_string"];
You should probably implement some sort of cache so that you aren't parsing the whole file for each string lookup.
Note that if you are using genstrings and the other related command line tools, you can use the following option to specify the name of your custom lookup function:
Usage: genstrings [OPTION] file1.[mc] ... filen.[mc]
...[snip]...
-s substring substitute 'substring' for NSLocalizedString.
If these strings files are being uploaded to a server, you can improve performance in the app a bit by first converting them to the binary plist format (this is what Xcode would normally do during a build):
plutil -convert binary1 myStrings.strings
You will have to put together the pieces yourself. There isn't any built-in facility to do that. Your localizations can just be serialized dictionaries with the development language string as the key and the localized string as the value. You can serialize as a plist, which may be most convenient to edit, or using a keyed archive, which may be more compact.
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