2016-03-25 70 views
0

Objective-C代碼試圖轉換到斯威夫特2:轉換爲斯威夫特2

-(void)dataLoadMethod 
{ 
    //NSLog(@"data load method is displaying") 
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    [dateFormatter setDateFormat:@"HH.mm"]; 
    NSString *strCurrentTime = [dateFormatter stringFromDate:[NSDate date]]; 
    NSLog(@"Check float value: %.2f",[strCurrentTime floatValue]); 
    if ([strCurrentTime floatValue] >= 18.00 || [strCurrentTime floatValue] <= 6.00){ 
     UINavigationController *navigationController = (UINavigationController *)[self.storyboard instantiateViewControllerWithIdentifier:@"NightSkin"]; 
    [self presentViewController:navigationController animated:YES completion:nil]; 
    }else{ 
     UINavigationController *navigationController = (UINavigationController *)[self.storyboard instantiateViewControllerWithIdentifier:@"DaySkin"]; 
    [self presentViewController:navigationController animated:YES completion:nil];   

    dispatch_async(dispatch_get_main_queue(), ^{ 
       }); 

這是斯威夫特到目前爲止我的代碼:

func loadCreate() { 

    let dateFormatter = NSDateFormatter() 
    dateFormatter.dateFormat = "hh:mm" 
    dateFormatter.dateStyle = .ShortStyle 
    var strCurrentTime = dateFormatter.stringFromDate(NSDate()) 
    print(dateFormatter.stringFromDate(NSDate())) 
    NSLog("Check float value: %.2f", strCurrentTime) 
    if ([strCurrentTime Float] >= 18.00 || [strCurrentTime Float] <= 6.00) { 
      NSBundle.mainBundle().loadNibNamed("DaySkin", owner: self, options: nil) 
     self.addSubview(self.view); 
    } 
    else { 
     NSBundle.mainBundle().loadNibNamed("NightSkin", owner: self, options: nil) 
     self.addSubview(self.view); 
    } 
} 

從if語句有麻煩;用double賦值floatValue並顯示ViewController。
任何幫助,不勝感激。

回答

1

您的代碼錯誤地鑄造了strCurrentTime,出於某種原因,您將視圖控制器更改爲僅將視圖添加爲子視圖。

這是正確變換代碼:

func someFunc() { 
    let dateFormatter = NSDateFormatter() 
    dateFormatter.dateFormat = "HH.mm" 
    let strCurrentTime = dateFormatter.stringFromDate(NSDate()) 
    NSLog("Check float value: %.2f", CFloat(strCurrentTime)!) 
    if CFloat(strCurrentTime)! >= 18.00 || CFloat(strCurrentTime)! <= 6.00 { 
     let navigationController: UINavigationController = (self.storyboard!.instantiateViewControllerWithIdentifier("NightSkin") as! UINavigationController) 
     self.presentViewController(navigationController, animated: true, completion: { _ in }) 
    } else { 
     let navigationController: UINavigationController = (self.storyboard!.instantiateViewControllerWithIdentifier("DaySkin") as! UINavigationController) 
     self.presentViewController(navigationController, animated: true, completion: { _ in }) 
    } 
} 

您還應該使用let而不是var當你在重寫或重新初始化變量你沒有計劃。

+0

非常感謝。但是,現在我得到'使用未解析的標識符作爲自己'的錯誤。 – Bux

+0

@Bux你在哪裏放這個代碼呢?我相信這應該是裏面的一些'UIViewController'在那裏,你一定會有的'self' – KlimczakM

+0

目標C我把它: - } - (無效){loadCreate [自performSelectorInBackground:@selector(dataLoadMethod) withObject :零]; (IBAction)Wx:(id)sender { {self-loadCreate]; } } – Bux