将具有最接近日期的数组内的对象返回到NOW

时间:2014-04-07 00:22:13

标签: ios objective-c nsdate

我有一个带有对象的数组,每个对象都有一个名为“date”的属性我希望通过该数组找到与当前日期最接近日期的对象。

#import <Foundation/Foundation.h>

@interface FixturesObject : NSObject
@property (nonatomic, strong) NSString *homeGoals;
@property (nonatomic, strong) NSString *awayGoals;
@property (nonatomic, strong) NSString *homeName;
@property (nonatomic, strong) NSString *awayName;
@property (nonatomic, strong) NSString *round;
@property (strong, nonatomic) UIImage *homeLogo;
@property (strong, nonatomic) UIImage *awayLogo;
@property (nonatomic, strong) NSString *date;
@property (nonatomic, strong) NSDate *date2;
@property (nonatomic, strong) NSNumber *epoch;


+(FixturesObject *)fixtureFromXMLDictionary: (NSDictionary *)dict;

@end

#import "FixturesObject.h"

@implementation FixturesObject
@synthesize homeGoals, awayGoals, homeLogo, awayLogo, homeName, awayName, round, date, epoch, date2;


+(FixturesObject *)fixtureFromXMLDictionary:(NSDictionary *)dict{

    FixturesObject *object =[[FixturesObject alloc]init];

    object.homeGoals = dict[@"HomeGoals"];
    object.awayGoals = dict[@"AwayGoals"];
    object.homeName = dict[@"HomeTeam"];
    object.awayName = dict[@"AwayTeam"];
    object.homeLogo = [UIImage imageNamed:dict[@"HomeTeam_Id"]];;
    object.awayLogo = [UIImage imageNamed:dict[@"AwayTeam_Id"]];
    object.round = dict[@"Round"];
    object.date = dict[@"Date"];

    NSString * dateString = [NSString stringWithFormat: @"%@",object.date];
    NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
    NSDate* myDate = [dateFormatter dateFromString:dateString];

    object.date2 = myDate;
    return object;

}

@end

mainview.m

FixturesObject *closestObject;
NSTimeInterval closestInterval = DBL_MAX;

for (FixturesObject *myObject in newFixtureObjectArray) {
    NSTimeInterval interval = ABS([myObject.date2 timeIntervalSinceDate:[NSDate date]]);
    if (interval < closestInterval) {
        closestInterval = interval;
        closestObject = myObject;
                NSLog(@"My closest date: %@", myObject);
    }
}

根据要求更新,非常感谢你们的帮助!

2 个答案:

答案 0 :(得分:0)

您需要确保myObject.date是类NSDate的对象。如果它是另一种类型的对象,它将无法工作。如果myObject.date为nil,它也不会正常工作。

您报告的新错误([NSNull date2]: unrecognized selector sent to instance 0x102f20b50)对您报告的行没有意义。必须有其他事情发生。

您可以使用新代码更新原始问题,还会显示myObject课程的标题吗?

答案 1 :(得分:0)

我认为您最好使用sortedArrayUsingComparator对数组进行排序。

    NSDate *currentDate = [NSDate date];

    NSArray *sortedFixtureObjectArray = [newFixtureObjectArray sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {

        if (![obj1 isKindOfClass:[FixturesObject class]])
             return NSOrderedDescending;
        else if (![obj2 isKindOfClass:[FixturesObject class]])
            return NSOrderedAscending;

        FixturesObject *one = (FixturesObject *)obj1;
        FixturesObject *two = (FixturesObject *)obj2;

        double firstInterval = [one.date2 timeIntervalSinceDate:currentDate];
        double secondInterval = [two.date2 timeIntervalSinceDate:currentDate];

        firstInterval = (firstInterval < 0 ? -1 * firstInterval : firstInterval);
        secondInterval = (secondInterval < 0 ? -1 * secondInterval: secondInterval);

        if (firstInterval < secondInterval)
            return NSOrderedAscending;
        else if (firstInterval > secondInterval)
            return NSOrderedDescending;

        return NSOrderedSame;

    }];

这将生成一个排序数组,其中第0项是最接近当前日期的时间间隔。

您会注意到我在块外宣布currentDate。这样您就可以将所有日期与相同日期进行比较,而不是每次都将新日期进行比较。