我正在开发应用程序,并试图根据星期几实现不同的文本标签消息。恩。如果将目标C转换为Swift日期标签
星期一: “今天的营业时间为:上午8:00 - 下午6:00” 星期二: “今天的营业时间为:上午8:00 - 下午6:00” 周三:“今天的营业时间为:上午8:00 - 6: 00pm“ 星期四:”今天的时间是:上午8:00 - 下午6:00“ 星期五:”今天的时间是:上午8:00 - 下午6:00“ 星期六:”今天的时间是:上午9:00 - 下午2:00“ 星期日: “对不起,我们今天关闭”
的作品我的目标C代码:
NSDate* currentDate = [NSDate date];
NSTimeZone* currentTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
NSTimeZone* nowTimeZone = [NSTimeZone systemTimeZone];
NSInteger currentGMTOffset = [currentTimeZone secondsFromGMTForDate:currentDate];
NSInteger nowGMTOffset = [nowTimeZone secondsFromGMTForDate:currentDate];
NSTimeInterval interval = nowGMTOffset - currentGMTOffset;
NSDate* nowDate = [[NSDate alloc] initWithTimeInterval:interval sinceDate:currentDate];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEEE"];
NSString *dayName = [dateFormatter stringFromDate:nowDate];
NSString *timing = @"8:00am - 6:00pm";
NSString *description [email protected]"Today's Hours are";
NSArray *days = @[@"Monday",@"Tuesday",@"Wednesday",@"Thursday",@"Friday",@"Saturday",@"Sunday",];
for (NSString *str in days) {
if ([str isEqualToString:@"Saturday"]) {
timing = @"9:00am - 2:00pm";
}
if ([str isEqualToString:@"Sunday"]) {
description = @"Sorry we are closed today";
timing = nil;
}
if ([str isEqualToString:dayName]) {
NSLog(@"%@ : %@ :%@",str,description,timing);
//here you can write yourLabelname.text =[NSString stringWithFormat:@"%@ : %@ :%@",str,description,timing];
}
}
钙ñ我得到帮助将此转换为迅速。
谢谢。
等都不是代码的翻译服务。尝试在Swift中自己编写方法,然后询问具体问题是什么导致了你的困难。 – Abizern 2015-02-07 16:01:11
顺便说一句:你的时区/抵消杂耍看起来可疑复杂,你想用什么来实现? - 确定当前星期几的方法只适用于英语区域。在我的电脑上,'dayName'将是“Montag”,“Dienstag”,...更好地学习如何使用NSDateComponents。 – 2015-02-07 16:12:57