How to export data to an Xcode-project file - ios

Is it possible to export data to a Xcode-project file?
For example, I want to generate a new "hello.csv" file, using swift, that says "hello world"
into my Xcode Project locally but not in in-app document.

To put it simply, no. If you want to use databases in code, use .plist (Preference List) files. Unfortunately, I don't know how to do this in Swift, but I do know how to in Objective-C.
To start off, make a PLIST file in Xcode, and add your data to it. Remember the key name values.
In your .m file, add this code to create an NSMutableDictionary that you can modify in code later:
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"yourFile" ofType:#"plist"]];
Then, to pull data out of the dictionary you made (dict), do something like this. Switch data types as necessary.
NSString *helloWorld = (NSString*)[dict valueForKey:#"yourKeyName"];
(Note: the cast of NSString* is necessary because, otherwise, it will yell at you for having the incompatible type of id. In Swift, it's object. If you plan on modifying the string, then use NSMutableString.)
Then, to save it to the file, save the key/value pair in the dictionary:
[dict setValue:helloWorld forKey:#"yourKeyName"];
And finally, save the dictionary and overwrite the PLIST file.
[dict writeToFile:[[NSBundle mainBundle] pathForResource:#"yourFile" ofType:#"plist"] atomically:YES];
Note: the writeToFile method's atomically parameter is asking whether or not it has to write the file right away or after it does whatever it needs to (as in, it's not a high priority). I recommend setting this to YES or true.

Related

iOS read data from a bit more 'complex' .plist

I know this question had been asked for several times, but I have trouble reading data in Xcode iOS. The problem was to achieve an implementation without having the keys for NSDictionaries hardcoded in the code.
Let's say, we have this .plist:
This is my plist
In this case I want to access the name and address from the restaurants dynamically without accessing the data by a hardcoded string.
Background of this is that I try to access the data on every cell on a grouped UITableView for iOS.
Anyone has some good information about this?
Generally (normally I code in c#) I know how to access dictionaries. But in this case, there are nested dictionaries with arrays which get me really confused and also frustrating :(
NSString *path = [[NSBundle mainBundle] pathForResource:#"PropertyListName" ofType:#"plist"];
//Plist should exist in main bundle
NSDictionary *root = [[NSDictionary alloc] initWithContentsOfFile:path];
Then in your case your first value is a array in dictionary read that object in an array like,
NSArray * restaurantsArray = [names objectForKey:#"restaurants"];
Then so forth You know what type of object exist in the hierarchy just call respective methods of each,
like objectForKey for dictionary.
objectAtIndex for array.

What is the need to store the Content of plist file into an NSArray?

I am learning Objective-C language. I have covered the following questions:
What is plist?
How to create a plist file?
What are the representation of plist file?
But I did not find the answer of these questions:
Why do we store a plist in NSArray?
What is the need to store a plist into NSArray?
Can you please help me to clear my doubts?
Sometime you want to do something at runtime or create something base on something else.
Let's imagine you have a game with a different levels and you want the levels to be vary you can hard code the levels in code but much nicer would be store it in the file. iOS lets you very easily load plist to array or dictionary and that's the reason why iOS developers choose to use it.
// Example of plist file
Level1:
NumberOfEnemies: 6
ScoreTarget: 100
....
Level2:
NumberOfEnemies: 12
ScoreTarget: 120
....
When you load the game you can read the plist and you can load the level base on the content.
It's much easier to add another level or add more customisation to the plist (file) that go back to the code any do it there.
It's just one of the examples but you could use plist to do much more.
Plist are mostly configuration files or place to store some values that you know that will never change. One particular example of the plist file is info.plist where you define attributes for the application.
Plist are nothing else but the same old XML file with attributes and elements. That said, you actually access plist through dictionary, not array.
For your question why do we store it in dictionary, you use dictionary to access these values since there is no other way to extract the information from it.
One more thing, plists have their own restrictions regarding the type you can store: Array, Dictionary, String, Date, Data, Number and Boolean.
PList : is a "Property List" file, uses the XML format to store objects such as arrays, dictionaries, String, Data, Number and Boolean.
How to create :
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:#"myPlistFile.plist"];
if (![fileManager fileExistsAtPath: path])
{
NSString *bundle = [[NSBundle mainBundle] pathForResource:#”myPlistFile” ofType:#”plist”];
[fileManager copyItemAtPath:bundle toPath:path error:&error];
}
[myDict writeToFile:plistPath atomically: YES];
Representation : Based on what is stored in plist file .
Key and Value. (NSDictionaries)
Key and multiple value . (NSArray) etc..
What is PLIST?
Plist stands for Property List. Plist is used to store data in hierarchical manner.A Property List can contain containers and primitives.
Containers – Array, Dictionary
Primitives – Boolean, String, Number(Integer,Float), Date, Data
A Container can contain other containers and primitive data-types.
i.e. Dictionary can contain other Dictionaries,Arrays and Primitives. Array can contain other Dictionaries,Arrays,Primitives.
How to Create PLIST File?
STEP 1: Right Click the Project
STEP 2: Choose New File
STEP 3: Select Resource from left side of the Template.
STEP 4: Then Click Property List
STEP 5: Click NEXT button of the Bottom.
STEP 6: Give property List Name.
STEP 7: Finally Click OK
For more Reference please go through the below link
https://stackoverflow.com/questions/9044735/steps-to-create-and-edit-a-plist-file-in-xcode
What are the Representation of PLIST file?
A property list can be stored in one of three different ways:
1. in an XML representation,
2. in a binary format, or
3. in an “old-style” ASCII format inherited from OpenStep.
You can serialize property lists in the XML and binary formats. The serialization API with the old-style format is read-only
XML property lists are more portable than the binary alternative and can be manually edited, but binary property lists are much more compact; as a result, they require less memory and can be read and written much faster than XML property lists. In general, if your property list is relatively small, the benefits of XML property lists outweigh the I/O speed and compactness that comes with binary property lists. If you have a large data set, binary property lists, keyed archives, or custom data formats are a better solution.
I refer the link for Why do we store a plist in NSArray? and What is the need to store a plist into NSArray?
1.http://hayageek.com/plist-tutorial/
2.https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/PropertyLists/AboutPropertyLists/AboutPropertyLists.html
3.http://nscookbook.com/2013/02/ios-programming-recipe-13-using-property-lists-plists/
4.Getting plist data in a order

Editing .plist file through objective-c

I am currently making a tweak for personal use but I don't really know a hell lot about programming. I know it isn't a smart idea to make tweaks if you don't know how to program properly but I'm only making one small tweak so I don't wan't/have time to spend months studying objective-c.
Now the problem: I have a plist located here:"/var/mobile/Applications/F5E2CB37-FC1B-46BC-8264-C7E2F17CD0EC/Library/Preferences/nl.telegraaf.ipad.plist"
That .plist contains a lot of options and I only need to change one. But the value that I wanna change is located in a dictionary so you have something like this.
.plist ----> cells ---> dictionary named OpenUDID ----> value i wan't to change.
The dictionary in the plist has 4 items and i only wan't to change is the first one.
So I created a tweak and added this to it:
%hook SBLockScreenViewController //only for testing
NSMutableDictionary *settings = [NSMutableDictionary dictionaryWithContentsOfFile:
[NSString stringWithFormat:#"/var/mobile/Applications/F5E2CB37-FC1B- 46BC-8264-C7E2F17CD0EC/Library/Preferences/nl.telegraaf.ipad.plist"]]; //accessing the .plist
-(void)displayDidDisappear
{
[settings setValue:#"test" forKey:#"PaywallAuthMethodStringKey"]; //trying to change a value from the .plist doesn't work
//NOTE this isn't a value in the dictionary in the .plist
%orig;
}
%end
The problems I have is that I don't know how to edit a value in a .plist in code(I searched about it on google but none of the solutions worked)
Also I don't know how to get into the dictionary and than be able to change values in there.(I couldn't find any information on this on the internetz)

Load a list of localized strings at once in IOS

I want to show a table with the list of strings which are localized.
The straightforward method would be:
a) Point data source to my ViewController
b) Define an array
c) Allocate the array in my ViewController and init (arrayWithObjects) it with the strings from localized resources (NSLocalizedString)
d) Use this array in UITableViewDataSource delegated methods
Mainly my concern is item b). The construction looks quite heavy and I wonder whether I can somehow specify and load whole list of localized string at once.
Do you mean that you want to have an array that contains all the keys defined in your Localizable.strings file?
That's pretty easy actually, as .strings files are in a format that correspond the the Old-ASCII-Style for the plist format containing a dictionary (the enclosing curly braces that normally need to be present in this Old-ASCII-Style plist format to enclose a dictionary being silently ignored).
So you can simply load the content of your Localizable.strings file into a NSDictionary and you are good to go!
NSString* stringsPath = [[NSBundle mainBundle] pathForResource:#"Localizable" ofType:#"strings"];
NSDictionary* locStringsDict = [NSDictionary dictionaryWithContentsOfFile:stringsPath];
NSLog(#"locStringsDict = %#", locStringsDict);
NSArray* localizedStrings = locStringsDict.allValues;
NSLog(#"All localized strings in current language = %#", localizedStrings);
Of course if you only want some of the localized strings, you could:
Either put the strings you want in a separate ".strings" file (a.k.a. different Strings table)
Or make an NSArray of all the keys corresponding to the strings you need, and then loop thru them to build your NSArray of localized string for these keys, calling NSLocalizedString on each key entry
[EDIT]
Note that it seems that strings files are compiled into binary plists before being embedded in the final application (just tested it on a sample project). Thus once your application is running, the Localizable.strings file is a binary plist (namely [NSString stringWithContentsOfFile:stringsPath encoding:0 error:nil] will not return the original content of your Localizable.strings file but some binary data interpreted as some junk string, and the original file is not accessible anymore)
So if you want to keep the order of the keys, the only other option is to parse the original strings file before compilation, and not at runtime. The best choice for that is probably IMHO to convert the strings file into an XML Plist using plutil -convert xml1 Localizable.strings -o - and extract the keys from there, for example using some simple XSLT stylesheet. The stylesheet could even directly turn the extracted keys into some new XML plist that will represent an NSArray of those keys, in the expected order, so you can load this plist into an NSArray by code and use them at runtime.

Using NSKeyedArchiver to generate XML from NSManagedObjects held in an NSDictionary

I have an NSDictionary, which contains a bunch of NSManagedObjects.
I can then use NSKeyedArchiver to write this to an NSData object.
These are generated using this method. Which works fine and allows me to save a section of schema to disc and then read it back as a new set of objects in the core data model.
If I use either archivedDataWithRootObject:
or archiveRootObject:toFile:, as per the documentation
I can see that the format of the archive is NSPropertyListBinaryFormat_v1_0, whereas I want to serialise in NSPropertyListXMLFormat_v1_0, so that I can write my objects to a file and then process them elsewhere as plain old XML. (In fact I want to generate documents from them on a Windows based system.)
1) Is there a way I can do this? If so how?
2) Is there a better approach.
I want to maintain the serialised nature, since I also want to send the file back to the iOS device later and recreate the object model.
Create your own instance of NSKeyedArchiver with initForWritingWithMutableData:.
Set the format with setOutputFormat:NSPropertyListXMLFormat_v1_0.
Encode your root object with encodeObject:forKey:.
Call finishEncoding.
To unarchive the data you encoded in this way, you have to similarly instantiate an NSKeyedUnarchiver.
Thanks Ole! I was heading in that direction, but was not sure if it was the right way. Here is what I did in code in case it helps someone.
NSDictionary *dataAsDictionary=[self toDictionaryBlockingRelationships:blockRelationship];
NSString *savePath = [#"~/Documents/Saved.data" stringByExpandingTildeInPath];
NSMutableData *xmlData=[NSMutableData data];
NSKeyedArchiver *archive=[[NSKeyedArchiver alloc ]initForWritingWithMutableData:xmlData];
[archive setOutputFormat:NSPropertyListXMLFormat_v1_0];
[archive encodeRootObject:dataAsDictionary];
[archive finishEncoding];
if(![xmlData writeToFile:savePath atomically:NO]){
NSLog(#"Failed to write to file to filePath=%#", savePath);
}
[archive release];

Resources