【发布时间】:2014-05-31 17:54:52
【问题描述】:
基本上,我有一个可以包含多个 NSManagedObjects 的数组,我正在尝试对这些对象进行排序,对于那些有开始日期的对象,我想比较开始日期和现在或开始日期和结束之间的时间如果设置了日期。最后设置一个计时器以在一秒钟内刷新此信息。
我遇到的问题是比较时间时只返回第一个对象的值和开始日期。如果我使用开始日期添加另一个值,则时间设置为 0,并在我希望将它们添加在一起时重新开始。
如果您需要更多信息,请告诉我
我之前使用过 for(object *obj in Array),但它似乎有更多问题
int time = 0;
if([_ttimes count] != 0){
for(int i=0; i < [_ttimes count]; ++i){
TTime *tTime = [_ttimes objectAtIndex:i];
NSLog(@"time%i", i);
if(tTime.sDate){
NSCalendar *cal = [NSCalendar currentCalendar];
NSDate *date = [NSDate date];
if(tTime.eDate){
date = tTime.eDate;
}
NSDateComponents *component = [cal components:NSSecondCalendarUnit fromDate:tTime.sDate toDate:date options:0];
int tmpTime = [component second];
time = time + tmpTime;
}
}
_ticketTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(TotalWorkTime) userInfo:nil repeats:NO];
}
把方法改成这样:
-(void)TotalWorkTime{
double time = 0;
if([_ttimes count] != 0){
for(TTime *tTime in _ttimes){
NSDate *date = [NSDate date];
if(tTime.eDate){
date = tTime.eDate;
}
NSTimeInterval timerint = -[tTime.sDate timeIntervalSinceDate:date];
time = time + timerint;
}
NSLog(@"Time:%f", time);
}
}
这似乎返回了一个更准确的时间,但是谢谢 Zaph,但这仍然不能解决 time += timerint 无法正常工作的问题,每次我添加一个新对象时这个数字都会重置,它也只返回值最后添加的对象。
【问题讨论】:
-
你是对的,快速枚举不能用于用户创建的类,除非添加快速枚举支持,这通常很容易添加。
-
好的,我试试看
-
虽然这比设置日历要干净得多,但它会返回一个负值(问题不大),但它仍然不允许我添加对象时间
-
查看我添加的答案。
标签: ios objective-c date nsmutablearray