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?
答案 0 :(得分:10)
该错误具有误导性。问题是hour
是可选的Int
。
我会将代码更改为:
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"
}
}
答案 1 :(得分:0)
或者使用API返回单个非可选值
let hour = calendar.component(.hour, from: date)
switch hour { ...
顺便说一下components:from
已经在Swift 3中重命名为dateComponents:from
。这样可以避免额外的桥牌演员。