How to save and retrieve a simple value type persistently to the Documents folder - swift

I have been looking around and all the examples are very complicated to understand, they use Data Structures, JSON, etc.
I just want to save a file encoded with Data type (binary) for a Double (it could be an Int or a String) to the Documents folder in the App sandbox.
Any guidance or assistance will be appreciated, thanks

Related

How to get the date from a NSImage

I'm working on a Mac App. I am relatively new to cocoa development and I'm struggling with a problem. Is there a way, to get the creation date information from a NSImage using Swift? I couldn't find a way to do this. And is there a way to get other metadata (exif) from a NSImage as well?
Thank you!
If you mean a file creation date, not all images are associated with files and even those that are don't expose an API for querying what that file is or any information about it.
For the EXIF data, you can iterate over the representations of the image. If any of them are of class NSBitmapImageRep, you can query rep.valueForProperty(NSImageEXIFData) to get a dictionary of EXIF key-value pairs. The keys are documented here. Note that an image may have no bitmap image reps, may have one, or may have multiple. It's up to you to figure out what to do in each case.

Add document to document library with additional column data using Powershell

I'm trying to loop through pdf files in a directory and send them to a sharepoint document library. When I send them I would like to add the customer, invoice, etc to the list as well. Anyone have recommendations?
Sure. This can done fairly easily. Here's the article I've used in the past for reference:
http://blogs.technet.com/b/heyscriptingguy/archive/2010/09/23/use-powershell-cmdlets-to-manage-sharepoint-document-libraries.aspx
Setting metadata should be pretty easy as well, but PowerShell can't guess what a customer, invoice, etc is. So you'll have to have some data source. If the filename contains the data, you could split it. If the data is in the file itself, there are some methods of getting plaintext strings out of a PDF, but it's going to be a bit harder than the first part of your request.
Let me know if I can help further with any specifics.

Exporting Data to Mail in CSV format

I have a list of Expenses Which include date,categoryName and Amount stored in Coredata.
I want them to be transferred in CSV Format to Mail.Can this be done?.I also want only the data in the current month to be sent.
I googled it and all i found is CHCSV parser and i have no idea on how to use it.
Converting your data to csv should be easy, just declare a mutable string, iterate over the objects you want to add and add the values you need to the string with comma seperation (best to enclose the data values in "", too). You can google on the csv format in wiki.
Then you write that string to a temp .csv file.
Then, using mfmailcomposer, you attach the .csv-file to the mail and you're done.
If you don't come up with a solution by googling these pieces of information, ask a more specific follow-up question here. But there are many resources on mfmailcomposer with examples out there.
Cheers

How to save / reload a custom array to a plist

I'm loading in data from an sqlite database, storing the values i load from there in the instance variables of a custom class, and then adding this class to a mutable array, which i'm then assigning to the instance variable of my view controller, for use in a tableview.
I would, though, like to save this array into a .plist file in the documents directory on the app's first run, so that i can retrieve the whole object from there on load, rather than pulling all 214 items from the database.
Is this approach a better option? if so, could someone please help provide me with some code that will allow me to save an array of my custom classes as a .plist file? I've come across a lot of sample code on the web, but none of it works correctly.
I'd like to:
Check for the existence of the my_data.plist file.
If it exists, read it in as the array.
If it doesn't, read the data from the sqlite db into an array.
save this data to a .plist so that it can be read in faster later.
Thanks guys, appreciate any help you can give me.
It will probably be faster to just get the values from your database on launch. There will almost definitely be more cost to parse a plist containing these values than to just get them all from the database, unless the query you have to use to get them from the database is really slow.
Note also that once you're saving these objects to a plist on disk, you're actually going to be hurting performance of your program because you'll be writing your objects to disk twice and reading them from disk twice. You'll also be introducing opportunities for discrepancies between the plist and the database in the event of a bug or a crash.
That said, the only way to prove this to yourself may be to implement and profile both options, and compare actual numbers. Check out #occulus's link, above, for instructions how to read and write a plist. To profile your app, try using Instruments
When I google for "nsarray writetofile custom object" (no quotes) and click on the first link in the results, I find a really useful page.
For the record, it's this:
http://www.cocoabuilder.com/archive/cocoa/240775-saving-nsarray-of-custom-objects.html

Property Lists: How to use it to provide easy exchangeable default data to the user?

I want to ship some default data with my app. This data can be stored perfectly in a property list. The strucure is simple:
Root
0
animalType = cat
animalName = Tom
1
animalType = dog
animalName = Rambo
I thought: When I use a property list rather than hard-coding it somewhere, then I could easily provide more defaults to choose from after the app is distributed already. If I would hard-code it, I would have to provide heavy upgrades every time, and Apple would take weeks to let them pass.
But there's one thing I don't get. I make that property list manually in Xcode and put that in my Resources group. As far as I know, Xcode would compile it into some binary format. In my app I would use NSPropertyListSerialization to create a NSDictionary out of it. But this property list would not get placed into the documents directory of the sandbox, right? So if the application would download an update of that property list some time in the future, the update would have to go into documents dir, but the app would still look at the old plist in the root, right? Where to put it? Must I copy it to documents, just like an sqlite database?
And the other thing: When I edit the plist and provide the whole thing as an XML for download/update from a server, then of course that thing would not be "compiled" into some binary format. How would that work? Would NSPropertyListSerialization have trouble reading it? Must I compile that thing every time with XCode and let the app download some binary stuff?
There are two commonly used property list formats: proprientary binary format and xml format (DTD). You can use either of them, and NSPropertyListSerialization will detect automatically, which one is used for your data when de-seralizing.
XML format is more verbose, but it's simple to generate. If you're publishing data from server, you might consider generating xml plist, and compress it with gzip or something.
Now to your first question about where to store the data. To make application payload smaller you might first check documents directory for updated plist, and if it is not present - load default plist from your application bundle.
One general approach used is to always copy plists or other updated elements into the application documents directory - then you just always load from there, and replace when there is an update.
Or you could pre-load the data into a database, download plist updates and refresh the database entries at that time.