Edit plist file in swift (not in bundle) - ios

beginner in Swift here.. I've already tried to look it up, but couldn't find an answer that worked.
I'm trying to write a macOS app, and I would like to know how to edit a plist file programmatically that is on the system but not bundled in the app.
Let's say, for example, we have a "contents.plist" file which is located in a folder in the system, and I want to access this file, then add keys and dictionaries to it, and save the changes in that file.
Any idea on how can I do that programmatically with swift ?
Thank you !
EDIT : PropertyListSerialization worked ! Thank you vadian 🙏
what I did to modify my external plist was to define a path in a constant and access it using FileManager.default.contents(atPath: myPath) instead of Bundle.main.path ..
Then use .add methods and .write to edit the plist .

Related

Create a plist outside Xcode?

In our app we have lots of photos that we need to read while using the app, we have to also orgenize them into groups/categories.
this option is to make a plist with their names, and just put them into the app and read the plist.
create folders inside the app, order them inside, and read the specific folder.
Option 2 took me days and I couldn't even make it work reliably, and it also seems not the right way to work for some reason.
Option 1, the problem is that if you have 100 files you have to edit your plist every time again for a new file you add.
Is there a way to make option 1 outside of xcode so I can put all files in a folder on my mac, get their plist, and put this plist inside Xcode ?
What's the right way to achieve this ?
plist is just a simple XML file. You can create the plist file and add to xcode as any normal file to the bundle and read it.
macOS comes with two command-line tools for manipulating plists:
/usr/bin/plutil can convert a plist between formats. You might like this because you could write your photo catalog in JSON using whatever tools you like, then convert it to a plist. Of course, then you could just use JSON directly in your app…
/usr/libexec/PlistBuddy (note that /usr/libexec is not normally in one's PATH) can modify a plist in place, adding, removing, or changing entries.
Both of these tools have man pages (man plutil, man PlistBuddy) and substantial built-in help (plutil -h, /usr/libexec/PlistBuddy -h).
First, you're making a false assumption. There is no need to use a plist file here. An ordinary text file listing the names will do just fine, and you can just make up your own format to dictate groups and suchlike. And that sort of file is trivially easy to maintain.
Second, your rather confused claim that "Option 2 took me days and I couldn't even make it work reliably, and it also seems not the right way to work for some reason" is just a cop-out. Folder references are not a difficult thing to use (you can configure them in the Finder, which is as simple as you can get), they do work just fine, and they are a perfectly reasonable solution here.

Updating plist file via code

I have created a plist file in the bundle, and I'm trying update the user's information into the plist. I know that I should copy the plist file from the bundle to the document directory, and edit it from there, but I still have a couple questions:
When I copy the plist file to the document directory, is it permanent? I mean, if I close the program and open it again, I can simply edit the plist file in the document directory, right?
If so, does it mean that I should only execute the code that copies the plist file to the document directory once the app is launched for the first time?
/main question/ Since I want a blank plist file for the user to update their data with, should I just simply create a plist file on the first launch? It'll be a lot easier without the copying around bundles and stuff.
I mean, what is the point of creating a plist file in the bundle in the first place? We will copying it into the document directory anyway, so why not just create one in code?
Yes. Writing a file is permanent, as long as you obviously don't delete/move the file somewhere else.
That is a valid option
Yes. If you need a blank file, you don't need it from the bundle. A better idea would be to create it when you need it (when there is something to write). Usually file creations are managed like so
Check if file exists
If not, create it.
Use the file.
If you need a template file (with already some stuff written in it), then a copy from the bundle is more appropriate. But even then, a lot of developers will like to do everything from code, it's not that heavy of a task, and it forces you to create/prepare the right objects and methods from the get-go.
Like Rooe N said, the NSUserDefaults IS a property list, so if you're talking about very simple data, say, like a username and a last-time-I-logged-In-date, you could store it there.
Note that NSUserDefaults are loaded all the way, every time you load the app, so you don't wanna use it as a database. But since you're going for .plist, I'll assume you've already ruled DBs out.
I'm not completely sure what you are trying to achieve, but you should think of plist as a place for global Constants not something that should be updated on runtime.
Maybe you should look at this:
NSUserDefaults

Localization error of App in ios

I've followed all the desired steps to achieve localization of application.following are those steps:
create a Localizable.strings file.
add languages which we are looking to localized.
add these in localizable.strings using add localized button.
Now I'm getting following alert message :
fatal: index file smaller than expected"
along with this the Localizable.strings file is not appearing in the list which asks about the reference file and resource language.anybody please help me as soon as possible.
Thanks in Advance.
This seems to be like a git error which can be fixed by removing .git/index and then reset to HEAD.
You can find details here:
http://programertools.blogspot.com/2014/04/how-to-fix-fatal-index-file-smaller.html
You can follow this Localization link .
Hope this helps
The index file has become corrupted, but it is easily re-creatable. Just remove it, and re-add the files in your working directory:
rm .git/index
git add .

C FOpen() Open Documents Directory iOS

I need help getting access to the documents directory using only C on iOS.
I have my .c file looking for a specific file in the application bundle. I have no problem accessing this file. It looks like this: fopen("filename",
Unfortunately, if I want to move that file to the documents directory, appending "/Documents/filename" doesn't work.
I know how to access the file using an objective-c class, easily, using filesystemrepresentation. But I don't know how to do it only in C. Any help would be greatly appreciated!
Thanks!
Fopen defaults to the directory that the executable file is in, ".app/" - on iOS.
I was able to get to the documents directory by making a char of the Current Working Directory and then removing the last couple characters of the char to get out of the ".app/" bundle and then appending the Documents path to the end of it.

Manually put Image Data to Plist file using Xcode

I want to put an Image in a plist file. Programmatically it is easy to save images as data in plist files, but I want to preset the data entry manually.
How can I put these data with my xcode plist editor in the data field?
Since you know how to do it in code, write some temporary code to write the desired image to a temp plist file. Then you can copy the entry into the real plist file.
However, I'd suggest a different approach. Store the actual image in the app's resource bundle and put the filename in the plist file. This will be so much easier to maintain and update.
You would have to get the binary data of the image you want to have in the property list, and then input it in the file.
I wouldn't recommend it though. You can easily achieve this programmatically, if you need help with the code, i can help you

Resources