【发布时间】:2011-05-16 13:48:54
【问题描述】:
我有这个代码: 我想在一年中的某一天存储一些 vales,我将时间段设置为例如 15/05/2011 到 20/05/2011
在 viewDidLoad 中: 我存储空值,然后我可以在数组中的任何地方存储一个值,我使用的是“哨兵值”:
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
好的,在这段代码中,我将一个值存储在数组“values”中,该值存储在数组“days”的一个索引中,该索引存储在数组“month”的一个索引中,我存储在数组“year”的一个索引中",它工作正常;但在此之后,如果我想将另一个字符串存储在同一位置的数组值中?
示例:我有另一个 NSString *string2 = "second",我想将此字符串存储在当天的同一位置,然后我想在同一天使用 "first" 和 "second" 的数组值,然后我不能做“[days replaceObjectAtIndex:j withObject: values];”但是我该怎么办?
【问题讨论】:
标签: objective-c arrays xcode ios replace