Using PLists for Persistence on iPhone

前端 未结 2 1240
南方客
南方客 2020-12-17 07:16

Simple question about property lists within an iphone app. I know you can read data in from a plist, but is there a way to write user-inputted data to a plist? If so, how? I

2条回答
  •  既然无缘
    2020-12-17 08:07

    This is how I write data items to a plist:

    [myPlistFile setInteger: myInt forKey: @"someKey"];
    

    Of course, you can change setInteger with setBool, etc for different types.

    Hope this helps!

    --

    Edit:

    If your .plist was a member of an important class or similar...

    Header of myClass:

    NSUserDefaults* myPreferences;
    @property (nonatomic, retain) NSUserDefaults* myPreferences;
    

    .m of myClass:

    self.myPreferences = [NSUserDefaults standardUserDefaults]; // load our preferences
    [[NSUserDefaults standardUserDefaults] registerDefaults: [NSDictionary dictionaryWithContentsOfFile: [[NSBundle mainBundle]pathForResource: @"nameOfFile" ofType: @"plist"]]]; // now load the custom .plist file
    

提交回复
热议问题