Different Property List format on IOS - ios

I'm facing an issue in using Property List. I've downloaded an example and the PList format is the following:
and using this code everything works fine:
NSString *path = [[NSBundle mainBundle] pathForResource:#"content_iPhone" ofType:#"plist"];
contentList = [NSArray arrayWithContentsOfFile:path];
Now, I'm trying to define a new PList by myself: I've created the new PList file in Xcode, and I've filled it on the User interface (following the previous plist structure), adding three dictionary items:
In this case, the same code doesn't work and analyzing the xml I see the following:
It seems that this is a dictionary of items instead of an array, as the first file.
Why? Can you please help me to create a correct property list?
Thanks in advance,
yassa

Use a text editor, change the first <dict> to <array>, change the last </dict> to </array>, delete all lines beginning <key>Item

Related

How to merge multiple plist files into one?

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!!

Trying to read InfoPlist.strings file... getting the key (?)

As many people don't know, the way to localize an application name on iOS and OSX is to add an InfoPlist.strings file to the bundle and localize that file. People mix this file with the Info.Plist file. Localization is not done into the Info.plist, is done in the InfoPlist.strings file.
Said that, I have created this file with two keys: CFBundleDisplayName and CFBundleName, as I always do. This works wonderfully and you can define different names for your app in different localizations. This file works seamlessly. You have to do nothing, just add the file to the project and localize it.
InfoPlist.strings is a strings file like this:
"CFBundleDisplayName" = "My Localized App Name";
"CFBundleName" = "My Localized App Name";
For this project in particular I have also to read the value of CFBundleDisplayName at run time.
I have tried to use this code:
NSString *appName = [[NSBundle mainBundle] localizedStringForKey:#"CFBundleDisplayName" value:nil table:#"InfoPlist"];
to read the CFBundleDisplayName key but the value I get back is CFBundleDisplayName. In other words, I provide the key and receive the key back, not the value. I should receive My Localized App Name.
What am I missing?
I did this exactly in Swift, and it works as expected.
let appname = NSBundle.mainBundle().localizedStringForKey("CFBundleDisplayName", value: nil, table: "InfoPlist")
but, if I delete de localisation string from the corresponding InfoPlist.strings file, it returns "CFBundleDisplayName".
So, check that you are not missing something in your .strings files
After trying several possibilities..
The BUG was "A second copy of InfoPlist.strings inside the project"

What is Plist file in detail?

I am learning iOS. I have a confusion about the Plist file, about the creator, applicable platform, format and so on. So what the Plist file really is?
PList is a property list.
You can find more useful information at:
https://developer.apple.com/library/ios/documentation/General/Reference/InfoPlistKeyReference/Articles/AboutInformationPropertyListFiles.html
http://nscookbook.com/2013/02/ios-programming-recipe-13-using-property-lists-plists/
How to use pList in iOS Programming
And the following one would give you more infor about the specific keys:
https://developer.apple.com/library/ios/documentation/General/Reference/InfoPlistKeyReference/Introduction/Introduction.html
A plist file is a property list. You can either create it using the nice animations Xcode gives you, or you can create pragmatically using XML. A plist file is something that can store objects (string, bool, data, date, number), like a database. And you can run through the plist file to retrieve or store the information just like a database.
In games you mostly save your score using NSUserDefaults as the data isn't sensitive, however saving information like a home address in NSUserDefaults isn't the best idea. Instead you'd rather want to save the information in a database - a plist file. Apple uses plist files in their apps. When you open contacts the information is retrieved from the plist and then put into a UITableView. When you click on a person it gives you their details, the details which were received from the plist file.
Another great things about a plist file is that you can change it from binary to XML and vice versa. Why would you ever want to change it to binary? Sometimes when you're dealing with large data e.g. a whole dictionary, it'll be faster to run through the data is binary than it would be in XML. To change it into binary, you go to terminal and use this command, plutil -convert binary1 yourFile.plist. To change binary to XML you use this command, plutil -convert xml1 yourFile.plist.
A plist in raw XML looks like:
A plist with the nice animation in Xcode looks like:
And finally a plist in binary looks like:
Now lets say you've created your plist and stored all the information in it that you want. To retrieve this information (in objc) use the following code.
NSString *path;
path = [[NSBundle mainBundle] pathForResource:#"file" ofType:#"plist"];
NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:path];
for (NSString *str in array) {
#autoreleasepool {
NSLog(#"%#", str);
}
}
Hope this helped you!!

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.

Where can I store my webserver URLs?

I hard code all URLs in my mobile app which I know is not a good idea. For example, if I were to ever need to move domain names I'd need to rename all URLs within the app from www.oldname.com to www.newname.com on each and every page.
I'm not sure where and how to store them so that I can modify this information on a single page and have it automatically changed across all pages.
In PHP I'd create a single file with variables that'd I'd include in all pages. I'm not sure how this is done in objective-c however. How do you do it?
You can create a constants file and include that in all other file where the URL is listed.
Create a file, e.g. constants.h where you put all your contstants.
In constants.h you could put something like this:
#define kApiBaseUrl #"https://myapi.mydomain.com"
And in your other files you acces it like this example:
NSURL *url = [NSURL URLWithString:kApiBaseUrl];
Probably this is like what you did use to do in PHP.
You can also set predefined URL's in the PLIST of your app but this is probably easier.
similar to edwardmp's response, you can also create a Constants.h file and define some static constant strings.
static NSString *const kSiteRootURL = #"http://www.someurl.com";
Check the folder "Supporting Files" inside your project you should have a file named "nameOfYourApp-Prefix.pch" then you can define the root URL for exemple
#define ROOT_URL #"yourwebsite.com"
and you can access ROOT_URL from anywhere in your project
You can keep your URLs in a property list file (File => New file => Property ...). You will have something like "config.plist". Similar with other property file, it is key => value file where the value can be a string, an arrays, etc.
Use NSBundle to load the file in your app, like
NSString *path = [[NSBundle mainBundle] pathForResource:#"config" ofType:#"plist"];
NSDictionary *config = [[NSDictionary alloc] initWithContentsOfFile:path];
This will give a benefit where you don't have to re-compile again your codes whenever you make any change on the URLs.
You can add a NSString to your .pch file to be included in all of modules.

Resources