Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS向Plist文档根添加数组_Ios_Objective C_Arrays_Object_Plist - Fatal编程技术网

iOS向Plist文档根添加数组

iOS向Plist文档根添加数组,ios,objective-c,arrays,object,plist,Ios,Objective C,Arrays,Object,Plist,好的,在收到反馈之后,我对我的代码做了这些修改,它正在工作,但只是添加和替换了第一个 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; NSString *user_id = [prefs objectForKey:@"

好的,在收到反馈之后,我对我的代码做了这些修改,它正在工作,但只是添加和替换了第一个

- (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;
}
)

声明您的代码,myslections是空的,我猜您想添加innerDic;此外,您正在保存mySelections而不是userSelections。 您应该将innerDic添加到mySelections并保存用户选择以解决问题

编辑 而不是

self.mySelectionsArray = [NSMutableArray arrayWithObject:[NSDictionary   dictionaryWithDictionary:innerDic]];
如果要覆盖读取数组,请添加对象:

[self.mySelectionsArray addObject:innerDic];
self.mySelectionArray必须是可变的,因此

self.mySelectionsArray = [NSArray arrayWithContentsOfFile:selectedPath];


要将对象添加到NSMutableArray,请使用[mysAddObject:YOUR_OBJ_REF];是的,这只是替换了整个对象,然后首先将已经存储的数据从plist获取到
mySelections
,然后将新数据添加到
mySelections
array。@user3008745您是要创建一个包含用户选择的多个玩家的plist,还是要创建多个包含单个玩家的plist?我正在向同一plist添加更多玩家。好的,我现在这样做:innerDic=[NSDictionary Dictionary Dictionary WithObjects:[NSArray arrayWithObjects:avatar,avatarSmall,first_name,last_name,nil]forKeys:[NSArray arrayWithObjects:@“avatar”,“avatar_small”,“first_name”,“last_name”,nil];self.mySelectionArray=[NSMutableArrayWithObject:[NSDictionary Dictionary WithDictionary:innerDic]];[self.mySelectionsArray addObject:innerDic];但它只是复制数组?首先,innerDic是tempDic的副本,在这种情况下是无用的。此外,每次都创建userSelections,因此它不会保留以前的值:我认为在添加更新的数组之前,您需要读取以前的保存值。是的,这肯定与此有关,有任何线索吗?请检查我编辑的问题代码。
self.mySelectionsArray = [[NSArray arrayWithContentsOfFile:selectedPath] mutableCopy];