[英]Parse, How to send array entries as multiple PFObjects, Create a new row for each array object
[英]Create new array entries for repeating dates
我正在解析一個.ics文件,我有一個名為rrule的屬性。 我成功解析並添加到數組中的對象是具有多個屬性的對象。
rrule屬性(對象的屬性之一)是一個NSString
並提供以下信息: Rrule:FREQ=WEEKLY;UNTIL=20140322;INTERVAL=1;BYDAY=FR
現在,我想在數組中為每個新條目添加具有相同信息的另一個對象,直到到達“ UNTIL DATE”,只有startDate和endDate應該更改。 在上面的示例中,從28. Feb開始,將為7.3和14.3和21.3添加對象。 22.3是星期六,在這一天之后,沒有其他星期五可以添加。 小時,分鍾和秒也應保持不變。
有誰可以幫助我嗎? 我設法解析了.ics文件中的信息,但是我很難實現這一信息,因此我可以真正使用解析后的信息。
數組中對象的其他屬性稱為:
NSString *summary, NSString *location, NSDate *startDate, NSDate *endDate, NSString *rrule, NSString *category, BOOL isExam
編輯:好的,這是我根據您的答案嘗試過的(展台):
NSDictionary *stringToNumber = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:2],@"MO",
[NSNumber numberWithInt:3],@"TU",
[NSNumber numberWithInt:4],@"WE",
[NSNumber numberWithInt:5],@"TH",
[NSNumber numberWithInt:6],@"FR",
[NSNumber numberWithInt:7],@"SA",
[NSNumber numberWithInt:1],@"SU",
nil];
for(DECalEntry *entry in planEntries) {
if(entry.rrule.length > 1) { //rrule is not empty
NSDateComponents *week = [[NSDateComponents alloc]init];
[week setWeek:1];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comp = [[NSDateComponents alloc] init];
NSNumber *number = [stringToNumber objectForKey:[self stringBetweenString:@"BYDAY=" andString:@"" andString:entry.rrule]];
[comp setWeekday:[number intValue]];
NSDate *newStartDate = [gregorian dateByAddingComponents:comp toDate:entry.startDate options:0];
NSDate *newEndDate = [gregorian dateByAddingComponents:comp toDate:entry.endDate options:0];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyyMMdd"];
NSDate *untilDate = [df dateFromString: [self stringBetweenString:@"UNTIL=" andString:@";" andString:entry.rrule]];
while([newStartDate compare:untilDate] == NSOrderedAscending) {
DECalEntry *newEntry = [[DECalEntry alloc]init];
newEntry.summary = entry.summary;
newEntry.location = entry.location;
newEntry.isExam = entry.isExam;
newEntry.rrule = @"";
newEntry.startDate = newStartDate;
newEntry.endDate = newEndDate;
newEntry.category = entry.category;
[self.helpArray addObject:newEntry];
newStartDate =[gregorian dateByAddingComponents:comp toDate:newStartDate options:0];
newEndDate =[gregorian dateByAddingComponents:comp toDate:newEndDate options:0];
}
}
if([self.helpArray count] > 0) {
for(DECalEntry *entries in self.helpArray) {
[planEntries addObject:entries];
}
}
和stringBetweenSting方法:
-(NSString*)stringBetweenString:(NSString*)start andString:(NSString*)end andString:(NSString *)string{
NSScanner* scanner = [NSScanner scannerWithString:string];
[scanner setCharactersToBeSkipped:nil];
[scanner scanUpToString:start intoString:NULL];
if ([scanner scanString:start intoString:NULL]) {
NSString* result = nil;
if ([scanner scanUpToString:end intoString:&result]) {
return result;
}
}
return nil;
}
EDIT2:好的,我終於開始工作了。 問題是,每天都有一條規則,而不是每周。 這導致while循環永不終止。 現在一切正常,再次感謝!
您可以使用NSDateComponents
和NSDate
進行此操作。 第一步是將規則解析為適當的NSDateComponents
以將其添加到開始日期。 我不知道您使用的規則的完整語法,但是對於示例,您給出的組件將由一周組成,因為頻率為每周:
NSDateComponents *week = [[NSDateComponents alloc] init];
[dc setWeek:1];
由於您已經知道間隔開始的時間,因此計算的開始日期應該是startDate
之后的第一個星期五。 要使用公歷查找任意日期之后的第一個星期五:
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *fri = [[NSDateComponents alloc] init];
[fri setWeekday:6];
NSDate *firstFridayFollowing = [gregorian dateByAddingDateComponents:fri toDate:startDate options:0];
知道第一個星期五后,您可以增加幾個星期:
NSDate *oneWeekLater = [gregorian dateByAddingDateComponents:week toDate:firstFridayFollowing options:0];
這樣很容易做到,直到下一個生成的日期晚於您的結束日期為止。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.