سؤال

Is there a quick way in Objective-C of identifying NSDate's in an NSArray that have a time of day after a given time (e.g. 8pm)?

I can't quite see anyway other than manually walking through each NSDate in the array and then using NSDateComponents to break out the hour/minute/second...Not even sure if there is a simple way to get the time from an NSDate in a fashion that represents a fraction of 24hours, as this might help a little. (e.g. 6pm would be 18/24 = 0.75 in this case)

هل كانت مفيدة؟

المحلول

There is no need to break in NSDateComponents.

NSTimeInterval interval = [date1 timeIntervalSinceDate:date2];
if (interval > 0) {
    // date2 is earlier
} else {
    // date1 is earlier
}

Now you can represent your target time(8 P.M., for example) with date2 and compare all dates of array with that.

نصائح أخرى

Haven't tried this myself, but I guess

- (NSArray *)filteredArrayUsingPredicate:(NSPredicate *)predicate

is what you're looking for.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top