At one point before Swift 3 this code would work without any compile errors however after converting to Swift 3 this code isn't compiling and I don't completely understand how the value types are different.
The error given is expression pattern of type 'CountableClosedRange<Int>' cannot match values of type 'Int'
for the switch cases where I'm attempting to use different ranges of values in the switch statement:
switch hour {
case 0 ... 11:
greetingStatement.text = "Good Morning"
case 12 ... 24:
greetingStatement.text = "Good Evening"
default:
greetingStatement.text = "Hello"
}
Here is the full code:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var userInput: UITextField!
@IBOutlet weak var usertOutput: UILabel!
@IBOutlet weak var tapMeAfterEnteringYourName: UIButton!
@IBOutlet weak var greetingStatement: UILabel!
@IBOutlet weak var letsGetStartedLabel: UILabel!
@IBOutlet weak var ballonsImageView: UIImageView!
@IBOutlet weak var teacherPointingToBoardImage: UIImageView!
@IBAction func setOutput(_ sender: AnyObject){
greetingStatement.isHidden = false
usertOutput.text=userInput.text
tapMeAfterEnteringYourName.isHidden = true
userInput.isHidden = true
letsGetStartedLabel.isHidden = false
ballonsImageView.isHidden = false
let date = Date()
let calendar = Calendar.current
let components = (calendar as NSCalendar).components([.hour], from: date)
let hour = components.hour
switch hour {
case 0 ... 11:
greetingStatement.text = "Good Morning"
case 12 ... 24:
greetingStatement.text = "Good Evening"
default:
greetingStatement.text = "Hello"
}
view.backgroundColor = UIColor.magenta
teacherPointingToBoardImage.isHidden=true
var timer = Timer.scheduledTimer(timeInterval: 1.4, target: self, selector: #selector(ViewController.updateViewController), userInfo: nil, repeats: false)
let hover = CABasicAnimation(keyPath: "position")
hover.isAdditive = true
hover.fromValue = NSValue(cgPoint: CGPoint.zero)
hover.toValue = NSValue(cgPoint: CGPoint(x: 0.0, y: -100.0))
hover.autoreverses = true
hover.duration = 5
hover.repeatCount = Float.infinity
ballonsImageView.layer.add(hover, forKey: "myHoverAnimation")
}
func updateViewController() {
print("Timer just fired")
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let resultViewController = storyBoard.instantiateViewController(withIdentifier: "MadLibOneViewController") as! MadLibOneViewController
self.present(resultViewController, animated:true, completion:nil)
}
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor=UIColor.purple
usertOutput.textColor=UIColor.white
userInput.textColor = UIColor.purple
greetingStatement.isHidden = true
letsGetStartedLabel.isHidden = true
ballonsImageView.isHidden = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
Can someone clarify that is simply a generic where any type of Int is allowable or expected?
The error is misleading. The problem is that hour
is an optional Int
.
I would change the code to:
if let hour = components.hour {
switch hour {
case 0 ... 11:
greetingStatement.text = "Good Morning"
case 12 ... 24:
greetingStatement.text = "Good Evening"
default:
greetingStatement.text = "Hello"
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With