2015-02-07 40 views
-3

我正在开发应用程序,并试图根据星期几实现不同的文本标签消息。恩。如果将目标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]; 
    } 
} 

钙ñ我得到帮助将此转换为迅速。

谢谢。

+2

等都不是代码的翻译服务。尝试在Swift中自己编写方法,然后询问具体问题是什么导致了你的困难。 – Abizern 2015-02-07 16:01:11

+1

顺便说一句:你的时区/抵消杂耍看起来可疑复杂,你想用什么来实现? - 确定当前星期几的方法只适用于英语区域。在我的电脑上,'dayName'将是“Montag”,“Dienstag”,...更好地学习如何使用NSDateComponents。 – 2015-02-07 16:12:57

回答

0

如果你想在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)") 
    } 
} 
+0

非常感谢 – user3062853 2015-02-07 17:06:49