将 Objective-C 转换为 Swift 日期标签
Convert Objective-C to Swift Date Label
我正在开发一个应用程序,并尝试根据星期几实现不同的文本标签消息。前任。如果
星期一:"Today's Hours are: 8:00am - 6:00pm"
周二:"Today's Hours are: 8:00am - 6:00pm"
星期三:"Today's Hours are: 8:00am - 6:00pm"
星期四:"Today's Hours are: 8:00am - 6:00pm"
星期五:"Today's Hours are: 8:00am - 6:00pm"
星期六:"Today's Hours are: 9:00am - 2:00pm"
周日:"Sorry we are closed today"
我的 Objective-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 =@"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 的帮助?
如果你想在 Swift 中编写原本在 objective-c 中编写的代码,这很容易。大多数时候,方法名称与 objective-c 中的名称相同。 In this Developer Library you can get many informations about that.
但为了让您了解一下这些方法有多么相似:
var currentDate:NSDate = NSDate()
var currentTimeZone = NSTimeZone(abbreviation: "GMT")
var nowTimeZone = NSTimeZone.systemTimeZone()
var currentGMTOffset = currentTimeZone?.secondsFromGMTForDate(currentDate)
var nowGMTOffset = nowTimeZone.secondsFromGMTForDate(currentDate)
var interval:NSTimeInterval = Double(nowGMTOffset) - Double(currentGMTOffset!)
var nowDate = NSDate(timeInterval: interval, sinceDate: currentDate)
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "EEEE"
var dayName = dateFormatter.stringFromDate(nowDate)
var timing = "8:00am - 6:00pm"
var description = "Today's Hours are"
var days = ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday", "Sunday"]
for str in days{
if(str == "Saturday"){
timing = "9:00am - 2:00pm"
}
if(str == "Sunday"){
description = "Sorry we are closed today"
}
if(str == dayName){
println("\(str) : \(description) : \(timing)")
}
}
我正在开发一个应用程序,并尝试根据星期几实现不同的文本标签消息。前任。如果
星期一:"Today's Hours are: 8:00am - 6:00pm"
周二:"Today's Hours are: 8:00am - 6:00pm"
星期三:"Today's Hours are: 8:00am - 6:00pm"
星期四:"Today's Hours are: 8:00am - 6:00pm"
星期五:"Today's Hours are: 8:00am - 6:00pm"
星期六:"Today's Hours are: 9:00am - 2:00pm"
周日:"Sorry we are closed today"
我的 Objective-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 =@"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 的帮助?
如果你想在 Swift 中编写原本在 objective-c 中编写的代码,这很容易。大多数时候,方法名称与 objective-c 中的名称相同。 In this Developer Library you can get many informations about that.
但为了让您了解一下这些方法有多么相似:
var currentDate:NSDate = NSDate()
var currentTimeZone = NSTimeZone(abbreviation: "GMT")
var nowTimeZone = NSTimeZone.systemTimeZone()
var currentGMTOffset = currentTimeZone?.secondsFromGMTForDate(currentDate)
var nowGMTOffset = nowTimeZone.secondsFromGMTForDate(currentDate)
var interval:NSTimeInterval = Double(nowGMTOffset) - Double(currentGMTOffset!)
var nowDate = NSDate(timeInterval: interval, sinceDate: currentDate)
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "EEEE"
var dayName = dateFormatter.stringFromDate(nowDate)
var timing = "8:00am - 6:00pm"
var description = "Today's Hours are"
var days = ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday", "Sunday"]
for str in days{
if(str == "Saturday"){
timing = "9:00am - 2:00pm"
}
if(str == "Sunday"){
description = "Sorry we are closed today"
}
if(str == dayName){
println("\(str) : \(description) : \(timing)")
}
}