Bux
Bux

Reputation: 1273

Convert to Swift 2

Objective-C code trying to convert to Swift 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(), ^{
               });

This is my code so far in Swift:

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);
    }
}

Having trouble from the if statement; assigning floatValue with double and presenting ViewController.
Any help is greatly appreciated.

Upvotes: 0

Views: 103

Answers (1)

KlimczakM
KlimczakM

Reputation: 12934

Your code was casting strCurrentTime incorrectly and for some reason, you changed presenting view controller to just adding the view as a subview.

This is correctly converted code:

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 })
    }
}

You should also use let instead of var when you have no plans in overriding or reinitializing you variable.

Upvotes: 1

Related Questions