Я просмотрел другие подобные вопросы об этом, но ничего действительно не сработало.writeToFile не работает с NSDictionary
Мне удалось написать только одну пару (объект/ключ) словаря (например: setObject: itemProperties [0] forKey [0]) на моем Plist. Но я хотел бы, чтобы все объекты и ключи были добавлены. Пока я не справился (вернул ошибку). Любая помощь?
// Path to Documents Folder
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"items.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath: path])
{
path = [documentsDirectory stringByAppendingPathComponent: [NSString stringWithFormat: @"items.plist"] ];
}
NSMutableDictionary *items;
if ([fileManager fileExistsAtPath: path])
{
items = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
}
else
{
// If the file doesn’t exist, create an empty dictionary
items = [[NSMutableDictionary alloc] initWithCapacity:5];
}
// array of the item properties
NSArray *itemProperties = @[myItem.itemTitle, myItem.itemImage, myItem.itemPositionX, myItem.itemPositionY, myItem.itemHeight, myItem.itemWidth];
// Set the key values for each field
NSArray *keys = @[@"Title", @"Image", @"PositionX", @"PositionY", @"Height", @"Width"];
[items setObject:itemProperties forKey:keys];
//Save dictionnary to Plist
[items writeToFile: path atomically:YES];
if (![items writeToFile:path atomically:YES]) {
NSLog(@"Error with creating Plist");
}
Спасибо за ваш ответ. Я просто проверил свои массивы, а некоторые объекты в этом списке отсутствуют. – Xavier