How to merge multiple plist files into one? - ios

Just to start, I really have no idea what Im doing. I was given this task for an internship, and am really learning as I go. I have multiple plist files, they consist of around 22 items each, and list values of colors. I need to merge all of these files into one, and am really not sure how to go about it. I have a certain structure I need to go by, and really Im not sure how to go about it. I was told to open the plists in texteditor and then paste all of the raw code into one text file, this doesn't seem to work as I only end up getting the values for the first plist I pasted into the text file. Any help would be nice. Thanks.

Assume your from.plist contains keys 1, 2 and to.plist contains 2, 3
Run this:
/usr/libexec/PlistBuddy -x -c "Merge from.plist" to.plist
to.plist will contain 1, 2, 3

There are a number of ways to handle this. By default a plist is a special form of XML file. If you figure out the syntax you can in fact use a text editor to merge the contents of multiple files together, but you need to make sure you get it right.
A plist file has a specific header for the entire file. You could not just copy/paste multiple plists together because then they would have that header repeated.
The next way to do it is programmatically. If you can figure out the type of outer collection these files contain (probably an array or a dictionary) then you could write a few lines of code that read in each of the plists as arrays, combines them using NSArray code (assuming they contain arrays of colors) and then save the combined array back to a new plist. As vadian says you can also use the NSPropertyListSerialization class. Thats a more general-pupose way of handling plist files, but it's also more complex and harder to figure out.
A third way to do it is in Xcode. If you right-click on a plist file and select "open in Xcode" it should give you Xcode's property list editor. You can then copy and paste the contents of the files together and save the results to a new file.

I figured it out!! First create the structure, or use the template given to you. I suggest opening this template/ structure in Xcode, as it makes it easier to switch between viewing the list as a plist and source code. Open your template as a source code. Then open each of your plists in text editor, and copy and paste the code from your plists into the appropriate area in your templates source code, then you can view it in Xcode as a property list to make sure it's correct. The only thing you have to be careful about here is making sure you are getting no errors. Otherwise this works great!!

Related

Add new values to XML dynamically

I have an XML file in my app resources folder. I am trying to update that file with new dictionaries dynamically. In other words I am trying to edit an existing XML file to add new keys and values to it.
First of all can we edit a static XML file and add new dictionary with keys and values to it. What is the best way to do this.
In general, you can read an XML file into a document object (choose your language), use methods to modify it (add your new dictionary), and (re-)write it back out to either the original XML file, or a new one.
That's straightforward ... just roll up the ol' sleeves and code it up.
The real problem comes in with formatting in the XML file before and after said additions.
If you are going to 'unix diff' the XML file before and after, then order is important. Some standard XML processors do better with order than others.
If the order changes behind the scenes, and is gratuitously propagated into your output file, you lose standard diffing advantages, such as some gui differs, and some scm diffs (svn, cvs, etc.).
For example, browse to:
Order of XML attributes after DOM processing
They discuss that DOM loses order where SAX does not.
You can also write a custom XML 'diff'er (there may be such off-the-shelf ... for example check out 'http://diffxml.sourceforge.net/') that compares 2 XML documents tag-by-tag, attribute-by-attribute, etc.
Perhaps some standard XML-related tool such as XSLT will allow you to keep the formatting constant without changing tag or attribute order. You'd have to research that.
BTW, a related problem is the config (.ini) file problem ... many common processors flippantly announce that the write-order may not agree with the read-order.

replacing NSDictionary with text file

We have a software that is in objective c, its running on the mac, but the question is for iOS devs as well.
the software is reading many values from NSDictionary like this :
[self.dic setObject:#"307146" forKey:#"somename-PHOTOGRAPHY"];
it than takes the object, and the key before the - and after the - ,and use it .
We would like to replace the dictionary with .txt(or similar)which will be outside the app, and each row will have a data structure similar to the line here,so we could read it , and have as little changes in code from the current implementation .
Any example of code to create and read lines from a txt would be great.
What you probably want is a plist file.
This is an xml file that can be edited using a number of different apps on a Mac. Also text edit (as long as you get the format right).
You can then load the file and it will create an NSDictionary for you that you can use without any code changes.

Reference Multiple files in the same schema.ini

I have several text files with tab delimited data. I want to have a single ini file for all of them.
Tried this:
[*.txt]
ColNameHeader=True
Format=TabDelimited
But that did not work.
The files are named:
one.txt
two.txt
etc
do this.
[one.txt]
Format=tabdelimited
[two.txt]
Format=tabdelimited
[three.txt]
Format=tabdelimited
Hope it works.
Regards.

Add many attributes to my xdatamodel (iOS Core Data)

I've got a list of 2000+, almost the same, 'NSString' attributes I need to "import" in my .xcdatamodel-file.
If I could open the xcdatamodel-file using an XML-like-text-editor, like a .plist-file, I can add all of my attributes, but I can't find a way how.
I managed to programmaticly create the attributes (using this tutorial), but then I can't set or fetch the attribute's data.
The list should look like:
["str_1_1"],["str_1_2"],["str_1_3"],...,["str_49_4"],["str_49_5"],...
Is there a way to programmaticly add attributes / set and fetch data from attributes?
OR
Is there a way to staticly add all possible attributes without clicking the +button over 2000 times?
You can just open the model file in any text editor. If you have the file compatibility for the file set to Xcode 4 or higher, it's even easy to edit.
The model Foo.xcdatamodel is actually a directory. Inside that is a file named contents, which is nicely formatted, easily readable XML. Edit that. A string attribute will look something like:
<attribute name="stringAttribute" optional="YES" attributeType="String" syncable="YES"/>
Add one or two string attributes in Xcode and then duplicate/edit them as needed.
A couple of notes:
Obviously, it's your job to get the syntax right. This is not documented but also not hard to figure out. If you end up with a broken model file that won't compile, you got something wrong.
It's probably a good idea to quit Xcode first. It might not freak out if you edit the model file while it's running, but you never know.
Having 2000+ string attributes is frankly terrifying and suggests an extremely bad data model. Before editing the model and adding all of these, please carefully consider whether there's a less extreme solution.

Filemaker: exporting a PLIST (for iOS)

I have a database which I want to export as an iOS compatible PLIST.
The work around I have come up with is to create a calculated field which adds the tagged padding and header and creates a report using these fields. I then export the preview of the report as a PDF, open the PDF in Acrobat Reader, select all text, copy and paste into XCode which recognises the PLIST format and all works as expected.
Is there a better way of doing this? (This seems a really convoluted way of doing things, high chance of error, etc.) The Export as XML option looks promising but I can't seem to join the dots.
Two ways that I can think of to do what you're trying to do. The most elegant way is probably the XML with XSLT export which you suggest. If you don't already know XSLT, though, you might try the following -- it sounds like with the calculated XML line you've already created, like this would be a simple change to your database:
Create a single new global field, say outputXML
Create a script, say plistCreator
In the plistCreator script:
Set outputXML to ""
Go to the first record you want to export
Loop through every record putting your calculated XML line into outputXML (set field outputXML to outputXML & ΒΆ & calculatedXMLLine)
Go to next record, exit after last
Export Field Contents (note that this is a different command than Export) for outputXML
The cleanest solution is to use the export XML with an XSLT for transforming the output. You'll need to know a little XSLT to do this, or at least be able to customize the examples from FileMaker.

Resources