Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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
Iphone 将字符串数组移植到每个包含字符串的字典数组_Iphone - Fatal编程技术网

Iphone 将字符串数组移植到每个包含字符串的字典数组

Iphone 将字符串数组移植到每个包含字符串的字典数组,iphone,Iphone,我试图将数组中的字符串移植到字典数组中,其中每个字典包含一个字符串和一个布尔值。希望这是有意义的,但我将尝试制作一个图表: 目前我有: <array> -<string1> -<string2> ... </array> - - ... 但我想: <array> -<dictionary1> --<string1> --<bool> -</dictionary1> -<dict

我试图将数组中的字符串移植到字典数组中,其中每个字典包含一个字符串和一个布尔值。希望这是有意义的,但我将尝试制作一个图表:

目前我有:

<array>
-<string1>
-<string2>
...
</array>

-
-
...
但我想:

<array>
-<dictionary1>
--<string1>
--<bool>
-</dictionary1>
-<dictionary2>
--<string2>
--<bool>
-</dictionary2>
...
</array>

-
--
--
-
-
--
--
-
...
我曾尝试创建一个循环来循环字符串数组,但它似乎不起作用

最终目标是将这个新的字典数组保存到NSUserDefaults(我也不太熟悉)。这就是我目前所拥有的,任何帮助都将不胜感激

// Get the current array from the user defaults.
NSArray *tempArray = [[NSUserDefaults standardUserDefaults] objectForKey:@"myArrayKey"];
self.myArray = [tempArray mutableCopy];

// Loop to cycle through the array of strings
for(int i = 0; i < [aList count]; i++)
{
    // Boolean to go in the dictionary with the string.
    checked = NO;

    // Create a dictionary, and set it with two objects and two keys. 
   //First object is the string in the array we are cycling through, the second object is the boolean.
    NSDictionary *tempDict = [[NSDictionary alloc] initWithObjectsAndKeys:[aList objectAtIndex:i], @"Title", checked, @"checked", nil];

    // Add this dictionary to the new array of dictionaries.
    [self.myArray addObject:tempDict];

    // This NSLog gives me 0 - but gives me  0 seven times (the number of items in the array I am cycling through) so it is definitely cycling through the array.
    NSLog(@"My Array Count: %i", [myArray count]);

    // Release the tempDict.
    [tempDict release];

}

// Write this new array of dictionaries back to NSUseDefaults.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:myArray forKey:@"myArrayKey"];
[defaults synchronize];
//从用户默认值获取当前数组。
NSArray*tempArray=[[NSUserDefaults standardUserDefaults]objectForKey:@“myArrayKey”];
self.myArray=[tempArray mutableCopy];
//循环遍历字符串数组
对于(int i=0;i<[aList count];i++)
{
//布尔值与字符串一起进入字典。
选中=否;
//创建一个字典,并将其设置为两个对象和两个键。
//第一个对象是我们循环使用的数组中的字符串,第二个对象是布尔值。
NSDictionary*tempDict=[[NSDictionary alloc]initWithObjectsAndKeys:[aList objectAtIndex:i],@“Title”,checked,@“checked”,nil];
//将此词典添加到新的词典数组中。
[self.myArray addObject:tempDict];
//这个NSLog给了我0-但是给了我7倍的0(我正在循环的数组中的项数),所以它肯定在数组中循环。
NSLog(@“我的数组计数:%i”,[myArray计数]);
//释放临时命令。
[临时释放];
}
//将这个新的字典数组写回NSUseDefaults。
NSUserDefaults*默认值=[NSUserDefaults standardUserDefaults];
[默认设置setObject:myArray forKey:@“myArrayKey”];
[默认同步];

插入一行怎么样

[tempDict retain];
下一个

[self.myArray addObject:tempDict]


正如Daniel指出的(我通过一些测试很快意识到),NSUserDefaults中的数组为零这一事实使它无法工作。

我不确定这是否是完美的答案,但它可能会解决问题。tempDict由alloc保留。addObject也保留了它。所以应该不需要额外的retain,只需要显示版本。属性myArray是如何声明的?你对此有什么建议吗?myArray和self.myArray是一样的吗?还有,tempArray是否存在?如果是nil,mutableCopy将产生nil,addObject将一事无成,[myArray count]将为零。是的,问题是因为tempArray实际上不存在,所以是nil。当我给它一些值,然后运行循环,它工作得很好。谢谢你的帮助,我想我知道该怎么做才能让它正常工作了。把它作为答案贴出来,如果你愿意,我会打勾的