iOS:更新 NSMutableArray
我有这个代码: 我想在一年中的某一天存储一些值,我
在 viewDidLoad 中设置了时间段,例如 15/05/2011 到 20/05/2011: 我存储空值,然后我可以在数组中的任何位置存储一个值,我使用“哨兵值”:
appDelegate.years = [[NSMutableArray alloc] init];
for (int i = 0; i < 50; i++) // I set 50 years
{
[appDelegate.years insertObject:[NSNull null] atIndex:i];
}
months = [[NSMutableArray alloc] init];
for (int i = 0; i < 12; i++)
{
[months insertObject:[NSNull null] atIndex:i];
}
days = [[NSMutableArray alloc] init];
for (int i = 0; i < 31; i++)
{
[days insertObject:[NSNull null] atIndex:i];
}
在我的方法中:
int firstDay = 15;
int lastDay = 20;
int firstMonth = 4;
int lastMonth = 4;
NSString *string = first; //this is the value that I want to store in the period
for (int i = firstMonth; i < lastMonth+1; i++)
{
for (int j = firstDay; j < lastDay+1; j++)
{
NSMutableArray *values = [[NSMutableArray alloc] init];
[values addObject:string];
[days replaceObjectAtIndex:j withObject: values];
[values release];
}
[months replaceObjectAtIndex:i withObject:days];
}
[appDelegate.years replaceObjectAtIndex:0 withObject:months]; //0 is 2011
好的,在这段代码中,我将一个值存储在一个数组“值”中,该数组存储在一个数组中我存储在数组“月”的一个索引中的数组“天”的索引,我存储在数组“年”的一个索引中的数组“天”的索引,它工作正常;但在此之后,如果我想在数组值中的同一位置存储另一个字符串?
示例:我有另一个 NSString *string2 = "second" 并且我想将此字符串存储在当天的同一位置,然后我希望在同一天将数组值与“first”和“second”一起存储,那么我不能执行“[天replaceObjectAtIndex:j withObject:values];"但我能做什么呢?
I have this code:
I want to store some vales in a day of the year, I set period for example 15/05/2011 to 20/05/2011
in viewDidLoad:
I store null value then I can store a value everywhere I want in the array, I'm using "sentinel values":
appDelegate.years = [[NSMutableArray alloc] init];
for (int i = 0; i < 50; i++) // I set 50 years
{
[appDelegate.years insertObject:[NSNull null] atIndex:i];
}
months = [[NSMutableArray alloc] init];
for (int i = 0; i < 12; i++)
{
[months insertObject:[NSNull null] atIndex:i];
}
days = [[NSMutableArray alloc] init];
for (int i = 0; i < 31; i++)
{
[days insertObject:[NSNull null] atIndex:i];
}
In my method:
int firstDay = 15;
int lastDay = 20;
int firstMonth = 4;
int lastMonth = 4;
NSString *string = first; //this is the value that I want to store in the period
for (int i = firstMonth; i < lastMonth+1; i++)
{
for (int j = firstDay; j < lastDay+1; j++)
{
NSMutableArray *values = [[NSMutableArray alloc] init];
[values addObject:string];
[days replaceObjectAtIndex:j withObject: values];
[values release];
}
[months replaceObjectAtIndex:i withObject:days];
}
[appDelegate.years replaceObjectAtIndex:0 withObject:months]; //0 is 2011
OK, in this code I store a value in an array "values" that I store in one index of array "days" that I store in one index of array "month" that I store in one index of array "year", it work fine; but after this, if I want store another string in array values in same position?
Example: I have another NSString *string2 = "second" and I want store this string in the same position of day then I want in same day the array values with "first" and "second", then I can't do "[days replaceObjectAtIndex:j withObject: values];" but what Can i do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我推断正确的话,您正在尝试在同一天存储第二个值,对吧?
如果没有特定需要按照当前方式布置数据结构,我强烈建议您仅使用 365 天的普通数组。您当前拥有的类似于树结构,这也很好,但用数组实现很痛苦(并且内存效率非常低)。
您似乎忘记了,一旦在树中的某个位置初始化了一个数组,您就可以简单地附加到该现有数组。
话虽如此,这是我根据您当前的解决方案的输入:
If I inferred that correctly, you are trying to store a second value at the same day, right?
If there's no specific need for laying out your data-structure the way you currently have, I would strongly recommend you just use a plain array with 365 days. What you currently have resembles a tree structure, which is fine too but a pain to implement with arrays (and very inefficient memory wise).
You seem to forgot that once you have an array initialized at a location in your tree, you can simply append to that existing array.
Having said that, here's my input based on your current solution:
我发现这种方法有两点问题 -
但是,如果您希望以相同的方式继续,则需要进行一些更改 -
对于值部分,
您还需要以类似的方式处理其他数组。
There are two things I find problematic with this approach -
However if you wish to proceed in the same way, you need to make some changes –
For the values part,
You will also need to deal with the other arrays similarly.