开发者

Property List management

开发者 https://www.devze.com 2023-01-11 07:02 出处:网络
I am trying to create an app that has a list of users and each user will be assigned an array of data that is editable.I am new to persistent data so i thought a property list would be the easiest to

I am trying to create an app that has a list of users and each user will be assigned an array of data that is editable. I am new to persistent data so i thought a property list would be the easiest to use. The first view of the app will have a list of the users that have already edited at least their name, and then have "New User" for each property list that has not yet had anything edited. (I am sure there is code to have the program create new property lists each time a person clicked on "New User" but that is pro开发者_如何学Cbably far too difficult for me so for the time being I have just simply defined 3 property lists that will correspond with my 3 users.)

So, for the 'viewDidLoad' portion of my apps main screen i need to populate a UIPicker with the names of the 3 users (or "New User" if there have not been any editing). I did this:

NSString *filePathForProfile1 = [self dataForProfile1];
NSString *filePathForProfile2 = [self dataForProfile2];
NSString *filePathForProfile3 = [self dataForProfile3];


if ([[NSFileManager defaultManager] fileExistsAtPath:filePathForProfile1]) {

    NSArray *arrayProfile1 = [[NSArray alloc]initWithContentsOfFile:filePathForProfile1];
    NSArray *arrayProfile2 = [[NSArray alloc]initWithContentsOfFile:filePathForProfile2];
    NSArray *arrayProfile3 = [[NSArray alloc]initWithContentsOfFile:filePathForProfile3];

    NSArray *array = [[NSArray alloc]initWithObjects:[arrayProfile1 objectAtIndex:0],[arrayProfile2 objectAtIndex:0],[arrayProfile3 objectAtIndex:0],nil];



    self.profileData = array;
    arrayProfile1.release;
    arrayProfile2.release;
    arrayProfile3.release;
}

Now, because I have been running the program, there is already a file saved for Profile1 so the picker does display the name for that one - but for the life of me I can't quite figure out how to have it display "New User" for the other 2. I tried to set up an if-then statement with that fileExistsAtPath argument to create init the array with "New User" but then I couldn't pass the array out of the argument. Help Please!!


I would recommend you to have a look at the Archives and Serializations Programming Guide. The most proper solution (well there is core data as well...) for your problem could be object serialisation.

You probably have an array of "Person"-Objects and the Person Class has a MutableArray of "Data"-Objects. All you have to do then is make the Person-Class and the Data-Classe(s) NSCoding compliant. Afterwards you can easily persist your Array of Persons by something as easy as : [NSKeyedArchiver archiveRootObject:yourPersonsArray toFile:yourFilePath];

also there is a usefull tutorial found NSCoding / NSkeyedArchiver Tutorial

cheeers,
sam

0

精彩评论

暂无评论...
验证码 换一张
取 消