Write NSArray to file on computer once - ios

I've got a large amount of data stored in a CSV file. I can parse the file into a NSArray, but the process takes about 1 minute on the phone. I can then save this file to the phone using
[NSArray writeToFile:filePath atomically:YES]
Is it possible for me to save this NSArray to a file on the computer, and then only have to pull the NSArray from a file in the appBundle rather than have to parse it on the phone?
In other words, I have a massive CSV file that I want to convert to a file in the appBundle than can quickly be turned into an NSArray. I don't want the app to go out with the need for this file to be parsed at all. So how do I make a file that is readable by [NSArray initWithContentsOfFile:] from my CSV?

Yes you can, with some restrictions:
In iOS, the app bundle is read-only. You could build up this array and save it to your bundle as part of creating your app, but you could not do that at runtime.
I sometimes write code that creates files in the documents directory in the simulator, then manually copy the files out of the simulator's sandbox directory for my app and into the project.
If you want to build an array and save it to disk at runtime, you will need to save it to one of the allowed directories in your sandbox - probably the documents directory.
Saving your array to a file will only work if the entire "object graph" of objects contained in the array are all "property list objects" (NSString, NSData, NSDate, NSNumber, NSArray, or NSDictionary objects). You can have an array of dictionaries of arrays wit strings, numbers, data, dates, etc, in any combination, but if there is even 1 object down inside one of the other objects, the save will fail.
Another point:
The NSArray method writeToFile:atomically: converts your array to an XML property list file. Note that XML property lists are bigger and slower to save and load than binary property list files.
Take a look at the NSPropertyListSerialization class reference for info on saving an object to a binary property list, which is more compact and faster.

With a Mac program, possibly a target in the iOS app you are writing:
Parse the CSV to an NSArray (you state you can do this)
Save the NSArray to a file from a Mac program using
- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)flag
Add it as a resources in Xcode. Load it from the app bundle with
+ (id)arrayWithContentsOfFile:(NSString *)aPath

Related

How to list all files for one target in Xcode

Is there a XC command I can use to list all files for one target?
Unfortunately, I'm unaware of any environment variables available to a build script.
The best way that I know of would be to open the pbxproj file inside your xcodeproj bundle and parse it. Luckily, it seems to be in a plain text format compatible with plists. You could invoke the plutil command to convert it to XML or json for another tool/command to consume if that is your goal. You can utilize a "Run Script" build phase if you need this list as part of your build. You can also instantiate an NSDictionary directly with that file if that is useful to you.
The plist is pretty simple. It consists of a root dictionary with some version strings and a giant objects dictionary. Each object has an isa type and the one you are interested in is the PBXNativeTarget type. Scan until you find the target with the correct value for the name key. Once you find your target look at its buildPhases; AFICT the first entry in buildPhases is the key for the corresponding PBXSourcesBuildPhase object. That build phase object has a files array which contains yet another set of IDs (one for each file compiled into the target) which point to a PBXBuildFile object, which has a fileRef string as a key to another object, this time to a PBXFileReference. This object finally has a path key which will be the path to the source file.
Wait... did I say "simple"?

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

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

querying documents directory objects ios

I have images in uitableview, they each have a string for they're path in documents directory.
Now my trouble is if somebody adds the same image they will have the same path.
I was thinking of making an if-statement that will run on all of my fetchedResultsController objects or better yet my entire documents directory and append a number or something to the pathString.
lets say user adds title.jpg to doc directory, then he adds the same image then I want a check to see if it already exists, if it already exists in doc directory then append title(1).jpg so it can save properly and so on.
any efficient way of doing that ?
Depending on the OS you're targeting you can set the image name using NSUUID. If you're targeting < iOS 6 you'll have to use CFUUIDRef. This will always ensure you have a unique filename for an image.

hexdump in Objective-C

I want to see the dump of my sqlite .db file inside my Xcode project like on a UILableView.
How can I run a command line command like hexdump from within my iOS app?
How can I run a command line command like hexdump from within my iOS app?
On MacOS X, you could do this using NSTask to run whatever program you want. You'd use NSPipe to send data to and/or get data from the command.
But you can't do that on iOS. First of all, the hexdump command may or may not exist in a standard iOS installation. Second, NSTask isn't available. If you really just want to dump the bytes from a file into a label, it's not difficult. I'd look at using NSInputStream to open the file and read its data, and then convert each byte to an appropriate hex string and add that to a larger string that'll accumulate the total dump. Once you're done, assign that larger string as the text of the label. Or, you could do the same thing yourself by just reading the file into an instance of NSData and iterate over the bytes yourself to produce the hex string.
Use NSData to read the file as raw data, then display the bytes however you want (for example, using a format string).
-actually from another user #jtbandes

Resources