I’ve this model:
@interface Data : NSObject
@property int Period;
@property (nonatomic, assign) NSDate *Start;
@property (nonatomic, assign) NSDate *End;
@end
@implementation Data
@synthesize Period, Start, End;
@end
I add data in mutable array.
@property (strong, nonatomic) NSMutableArray* myArray;
self.myArray = [NSMutableArray arrayWithCapacity:0];
Data *element1 =[ [Data alloc] init];
element1.Period = 1;
element1.Start = dateStart1;
element1.End = dateEnd1;
[self.myArray addObject:element1];
Data *element2 =[ [Data alloc] init];
element2.Period = 2;
element2.Start = dateStart2;
element2.End = dateEnd2;
[self.myArray addObject:element2];
Data *element3 =[ [Data alloc] init];
element3.Period = 3;
element3.Start = dateStart3;
element3.End = dateEnd3;
[self.myArray addObject:element3];
I can extract Start dates from array
Data * extractDateStart = [self.myArray valueForKey:@"Start"];
NSLog(@"All Start Dates: %@", extractDateStart);
I need to cycle for that array to search in which of the three period a specific date (inserted by user) is.
I know how to extract data from ‘normal’ array:
for(NSString * extractDate in self.myArray) {
NSLog(extractDate);
}
And know too how to compare one date with another:
switch ([dateInserted compare: date]) { case NSOrderedAscending: // dateInserted old than date break; case NSOrderedSame: // dateInserted the same then date break; case NSOrderedDescending: // dateInserted new then date break; default: // generic error break; }
But how to move in this case ?
I might not understand your question, but it sounds like you:
NSDateDataobject for which theNSDatefalls between theStartandEnddatesIf that’s the case, something like this should work:
For other good ways to test if a date falls between two other dates, see this question.