Is it possible to create mutable versions of objects read in from a plist

后端 未结 1 1210
刺人心
刺人心 2020-12-20 08:27

I\'m reading into my application a plist which at the top level is an array. It\'s simple enough to make sure the array starts as mutable

self.plistData = [         


        
1条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-20 09:05

    The simplest approach is to use NSPropertyListSerialization and passing the proper options to get mutable containers.

    NSString *path = [[NSBundle mainBundle] pathForResource:@"myDataSource" ofType:@"plist"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    NSMutableArray *array = [NSPropertyListSerialization propertyListWithData:data options:NSPropertyListMutableContainers format:nil error:nil];
    self.plistData = array;
    

    Ideally you will make use of the error parameter and do proper error checking but this will get you started.

    This code will load the plist and return a mutable array and every contained array and dictionary will also be mutable.

    0 讨论(0)
提交回复
热议问题