0

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:

  1. 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?

  2. 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?

  3. /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?

Community
  • 1
  • 1
Chandler
  • 25
  • 1
  • 7

2 Answers2

1
  1. Yes. Writing a file is permanent, as long as you obviously don't delete/move the file somewhere else.

  2. That is a valid option

  3. 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.

Gil Sand
  • 5,802
  • 5
  • 36
  • 78
  • I'm wondering how creating a blank plist file during initial launch would serve a useful purpose. Why not simply create the file if/when there's actually something to be saved? – jlehr Jan 06 '16 at 15:53
  • Thank you Zil and jlehr! That solves what has been bothering me for the past entire week! I guess the if(file does not exist){create file} logic works for my program better than the "create file on the first launch" logic. Thanks again! – Chandler Jan 07 '16 at 06:45
  • this logic is actually very important, I don't know why I didn't think of it at the time of writing the post. This concept applies to everything in general when it comes to programming : don't do something unless you really need to do it. With the exception of long running tasks that might be executed earlier because you don't want your user to wait more than a second. Otherwise, the latest is usually the better, keeps everything where it should and you don't waste resources. – Gil Sand Jan 07 '16 at 09:12
  • I agree. This logic is so neat and simple and yet I didn't even think about it before. Again, thanks for the great answers. I'll come back and upvote your answer when I gain enough reputation. – Chandler Jan 07 '16 at 12:08
-2

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

Community
  • 1
  • 1
Roee N
  • 502
  • 2
  • 4
  • 17
  • Thank you for answering Roee. NSUserDefaults is useful but in this case plist probably works better for my program. Thank again for the fast response – Chandler Jan 07 '16 at 06:47