如何将.hour()和.minute()从Swift转换为Objective-C?

时间:2016-04-14 15:29:45

标签: objective-c swift nsdate hour seconds

如何将.minute()和.hour()转换为Objective-C?

    channel.writeAndFlush(new DatagramPacket(Unpooled.wrappedBuffer(content), to));

我试过了:

func nearestHour () -> Int {
    let halfHour = NSDate.minuteInSeconds() * 30
    var interval = self.timeIntervalSinceReferenceDate
    if  self.seconds() < 30 {
        interval -= halfHour
    } else {
        interval += halfHour
    }
    let date = NSDate(timeIntervalSinceReferenceDate: interval)
    return date.hour()
}

我在这些方面遇到以下错误:

- (int) nearestHour {
NSInteger halfHour = [NSDate minuteInSeconds].intValue * 30;
NSTimeInterval interval = [self timeIntervalSinceReferenceDate];
if ([self seconds] < 30) {
    interval -= halfHour;
}
else {
    interval += halfHour;
}
NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate: interval];
return [date hour];
}

NSDate没有可见的@interface声明选择器秒。

if ([self seconds] < 30) {

NSDate没有可见的@interface声明选择器小时。

2 个答案:

答案 0 :(得分:0)

如前所述,NSDate没有您需要的界面。您需要将其分解为日历界面。然后你可以单独设置小时/分钟/秒等,或者获取它们或你需要做的任何事情。

NSDateComponents *components = [[NSCalendar currentCalendar] components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:self];

if ([components seconds] < 30) {

对于你的例子:

- (int) nearestHour {
    NSDateComponents *components = [[NSCalendar currentCalendar] components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:self];
    NSInteger halfHour = [NSDate minuteInSeconds].intValue * 30;
    NSTimeInterval interval = [self timeIntervalSinceReferenceDate];
    if ([components minutes] < 30) {
        interval -= halfHour;
    }
    else {
        interval += halfHour;
    }
    NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:    interval];
    return [date hour];
}

关于SO和Google的大量示例:How do I get the current hour using Cocoa?

https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/DatesAndTimes/DatesAndTimes.html#//apple%5Fref/doc/uid/10000039

答案 1 :(得分:0)

Apple建议始终使用NSCalendar进行日期数学运算,因为例如夏令时更改时,通过添加/减去秒计算的某些日期可能不存在。

nextDateAfterDate根据指定的日期组件计算下一个日期,在这种情况下,分钟为零的下一个日期。要获得过去最近的小时,您也可以向后搜索。

<强>夫特

let now = NSDate()
let calendar = NSCalendar.currentCalendar()
let roundDown = calendar.component(.Minute, fromDate: now) < 30
let components = NSDateComponents()
components.minute = 0
let options : NSCalendarOptions = roundDown ? [.MatchNextTime, .SearchBackwards] : .MatchNextTime
let nearestDate = calendar.nextDateAfterDate(now, matchingComponents: components, options:  options)!
let nearestHour = calendar.component(.Hour, fromDate: nearestDate)

<强>目标C

NSDate *now = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
BOOL roundDown = [calendar component:NSCalendarUnitMinute fromDate: now] < 30 ;
NSDateComponents *components = [[NSDateComponents alloc] init];
components.minute = 0;
NSCalendarOptions options = roundDown ? (NSCalendarMatchNextTime | NSCalendarSearchBackwards) : NSCalendarMatchNextTime;
NSDate *nearestDate = [calendar nextDateAfterDate:now matchingComponents: components options:  options];
NSInteger nearestHour = [calendar component:NSCalendarUnitHour fromDate: nearestDate];