iOS 将数组添加到 Plist 文档根目录

标签 ios objective-c arrays object plist

好的,那么在收到反馈后,我正在对我的代码进行此更改,它正在运行,但只是添加和替换第一个代码?

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    NSString *user_id = [prefs objectForKey:@"user_id"];
    NSString *fixture_id = [prefs objectForKey:@"fixture_id"];

    // Get path to documents directory
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                         NSUserDomainMask, YES);
    if ([paths count] > 0)
    {
        NSString  *selectedPath = [[paths objectAtIndex:0]
                                   stringByAppendingPathComponent:[NSString stringWithFormat:@"%@-%@",user_id, fixture_id]];

        // Read both back in new collections
        self.mySelectionsArray = [NSArray arrayWithContentsOfFile:selectedPath];

        NSLog(@"Players Selected = %@", self.mySelectionsArray);
    }


    NSDictionary *tempDic = [[[self.listOfPlayersArray objectAtIndex:indexPath.section] objectForKey:@"contacts"] objectAtIndex:indexPath.row];

    NSString *avatar = [tempDic objectForKey:@"avatar"];
    NSString *avatarSmall = [tempDic objectForKey:@"avatar_small"];
    NSString *first_name = [tempDic objectForKey:@"first_name"];
    NSString *last_name = [tempDic objectForKey:@"last_name"];

    NSDictionary *innerDic;

    innerDic = [NSDictionary dictionaryWithObjects:
                 [NSArray arrayWithObjects: avatar, avatarSmall, first_name, last_name, nil]
                                            forKeys:[NSArray arrayWithObjects:@"avatar", @"avatar_small", @"first_name", @"last_name", nil]];

    self.mySelectionsArray = [NSMutableArray arrayWithObject:[NSDictionary dictionaryWithDictionary:innerDic]];


//    [self.mySelectionsArray insertObject:innerDic atIndex:0];
    [self.mySelectionsArray addObject:innerDic];

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    if ([paths count] > 0)
    {
        // Path to save array data
        NSString  *arrayPath = [[paths objectAtIndex:0]
                                stringByAppendingPathComponent:[NSString stringWithFormat:@"%@-%@",user_id, fixture_id]];

        NSLog(@"Path: %@",arrayPath);

        // Write array
        [self.mySelectionsArray writeToFile:arrayPath atomically:YES];
    }

    [self dismissViewControllerAnimated:YES completion:nil];
}

现在正在检查数组并将其下拉然后添加对象

选择播放器后的plist是这样的;

{
    "first_name" = Ollie;
    "last_name" = Akroyd;
},
{
    "first_name" = Ollie;
    "last_name" = Akroyd;
}
)

最佳答案

声明你的代码,mySelections是空的,我猜你想向其中添加innerDic;此外,您正在保存 mySelections 而不是 userSelections。 您应该将innerDic 添加到mySelections 并保存userSelections 来解决您的问题。

编辑 而不是

self.mySelectionsArray = [NSMutableArray arrayWithObject:[NSDictionary   dictionaryWithDictionary:innerDic]];

覆盖读取的数组,添加对象:

[self.mySelectionsArray addObject:innerDic];

self.mySelectionsArray 必须是可变的,因此而不是

self.mySelectionsArray = [NSArray arrayWithContentsOfFile:selectedPath];

self.mySelectionsArray = [[NSArray arrayWithContentsOfFile:selectedPath] mutableCopy];

关于iOS 将数组添加到 Plist 文档根目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20396480/

相关文章:

ios - 自动无限 collectionView - 闪烁

iphone - 崩溃在 actionForLayer :forKey:

ios - 使用点击手势选择行

c - 在c中将文件输入读取到char数组中

ios - 当旋转到横向时,如何让 UIPageViewController 工作?

ios - 让按钮快速将文本添加到 UITextField

objective-c - 在使用额外行插入新记录的同时在编辑模式之间切换时如何使用核心数据并正确显示动画?

objective-c - Objective-C 中的消息分发成本

c++ - new datatype*[10] 如何返回指向指针的指针?

ios - RESideMenu-两个数组