在switch语句中的每个case声明之后,XCode指示应该有一个预期的模式吗?
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
switch segue.identifier {
case: "SegueToCommentRating"
let commentRatingViewController = segue.destination as! CommentRatingViewController
commentRatingViewController.post = self.post
commentRatingViewController.delegate = self
case: "SegueToLogin"
let loginViewController = segue.destination as! LoginViewController
loginViewController.managedObjectContext = managedObjectContext
case: "SegueToEditCommentOrRating"
let commentRatingViewController = segue.destination as! CommentRatingViewController
commentRatingViewController.post = self.post
commentRatingViewController.userWillEdit = true
commentRatingViewController.delegate = self
default:
break
}
}
Run Code Online (Sandbox Code Playgroud)
正确的代码是:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let string = segue.identifier {
switch string {
case "SegueToCommentRating":
let commentRatingViewController = segue.destination as! CommentRatingViewController
commentRatingViewController.post = self.post
commentRatingViewController.delegate = self
case "SegueToLogin":
let loginViewController = segue.destination as! LoginViewController
loginViewController.managedObjectContext = managedObjectContext
case "SegueToEditCommentOrRating":
let commentRatingViewController = segue.destination as! CommentRatingViewController
commentRatingViewController.post = self.post
commentRatingViewController.userWillEdit = true
commentRatingViewController.delegate = self
default:
break
}
}
}
Run Code Online (Sandbox Code Playgroud)
只需将“:”移到要比较的值之后