To make my code more readable & maintainable, what's the best way to use labels instead of hardcoded Ints for case labels in a switch statement with a control expression of type Int?
E.g., inside my SettingsTableViewController, I tried
enum Section : Int {
case Phone
case LogOut
case DeleteAccount
}
and in – tableView:didSelectRowAtIndexPath:
switch indexPath.section {
case .Phone:
// Push EditPhoneViewController
case .LogOut:
// Log out
case .DeleteAccount:
// Present action-sheet confirmation
}
but got the compile error
Enum case pattern cannot match values of the non-enum type 'Int'
In the switch you can't simply use indexPath.section as that is not the same type as your enum.
Use switch Section(rawValue: indexPath.section)!
Related
How can we handle the same case with different inputs from the response in Enum's
enum MyNotificationType: String, Codable {
case practice = "push"
case practice = "PracticeRecommendation"
case play = "PlayRecommendation"
case play = "pop"
}
Edit:
I need this because I've common practice image for
push, PracticeRecommendation
and play image for
PlayRecommendation, pop
Edit 2:
enum MyNotificationType: String, Codable {
case push = "push"
case practice = "PracticeRecommendation"
case play = "PlayRecommendation"
case pop = "pop"
}
And added a switch
private func showImage(_ type: MyNotificationType) {
switch type {
case .practiceRecommendation, .push:
self.typeImgView.image = UIImage(named: "Practice")
break
case .play, .pop:
self.typeImgView.image = UIImage(named: "Play")
break
}
}
To deal with Images
Hmm. You can use enums with associated types, though you wouldn't be able to statically ensure possible strings unless you change init(rawType) too. Though, I'm not sure what you are trying to achieve so there could be a different way
enum MyNotificationType{
case practice(value: String)
case play(value: String)
init(rawType: String){
//define what case based on string, do error if you can't handle or use a unknown case
}
}
I have some code in older version in swift, like bellow
let initVc:UIViewController? = self.initViewController!
switch initVc {
case let vc as ProductListViewController:
pageMenu?.moveToPage(0)
case let vc as OrderListViewController:
pageMenu?.moveToPage(1)
case let vc as CustomerListViewController:
pageMenu?.moveToPage(2)
default:
pageMenu?.moveToPage(0)
}
in above code self.initViewController is parent view controller from there this controller is pushed.
after updat to swift 2.0 gives and error, like bellow
Immutable value 'vc' was never used; consider replacing with '_' or removing it
Please suggest changes required to fulfil this requirement.
Equivalent code without assigning a value to an (unused) variable
would be:
switch initVc {
case is ProductListViewController:
pageMenu?.moveToPage(0)
case is OrderListViewController:
pageMenu?.moveToPage(1)
case is CustomerListViewController:
pageMenu?.moveToPage(2)
default:
pageMenu?.moveToPage(0)
}
using the "type-casting pattern" is <Type>.
I am implementing my own TableView because I would like to have special animation when reloading a row using reloadRowsAtIndexPaths:withRowAnimation.
The problem is that I need to give a type of animation when I'm calling this method. So I would like to know if it's possible to add an extra case to UITableViewRowAnimation enumeration?
No, it's not possible, for at least one reason: switch statements not using a default case wouldn't compile, because they would be missing the newly added case.
Consider this enum:
enum TestEnum {
case ONE
case TWO
}
and some code using it:
let testOne = TestEnum.ONE
switch testOne {
case .ONE:
println("one")
case .TWO:
println("two")
}
If you were able to add one or more cases in an extension:
extension TestEnum {
case THREE
}
then the switch statement written above wouldn't compile because the new case is not handled.
I am new to Swift and iOS development, so I am trying to build a calculator app for learning purposes. However, I am encountering an error. I have titled all of my buttons with the number they represent, so I am retrieving the title in the buttonPress IBAction via sender.titleLabel.text. Then, I pass that into a switch statement to determine if the button was a number or an operator.
func handleButton (sender:UIButton) {
switch sender.titleLabel.text {
case "1","2","3","4","5","6","7","8","9","0" :
println(sender.titleLabel.text)
default:
break
}
}
The error is that sender.titleLabel.text will not bind to the string values I have entered - nor any string values - even though it is is of type String.
There seems to be a bug in the complier at the moment where Implicitly Unwrapped Optionals cannot be used in switch statements. Instead you can use optional binding to satisfy the compiler. As an extra plus, this will handle the case where titleLabel.text is nil.
func handleButton (sender:UIButton) {
if let text = sender.titleLabel.text {
switch text {
case "1","2","3","4","5","6","7","8","9","0" :
println(sender.titleLabel.text)
default:
break
}
}
else {
// sender.titleLabel.text is nil
}
}
I am new to Swift and iOS development, so I am trying to build a calculator app for learning purposes. However, I am encountering an error. I have titled all of my buttons with the number they represent, so I am retrieving the title in the buttonPress IBAction via sender.titleLabel.text. Then, I pass that into a switch statement to determine if the button was a number or an operator.
func handleButton (sender:UIButton) {
switch sender.titleLabel.text {
case "1","2","3","4","5","6","7","8","9","0" :
println(sender.titleLabel.text)
default:
break
}
}
The error is that sender.titleLabel.text will not bind to the string values I have entered - nor any string values - even though it is is of type String.
There seems to be a bug in the complier at the moment where Implicitly Unwrapped Optionals cannot be used in switch statements. Instead you can use optional binding to satisfy the compiler. As an extra plus, this will handle the case where titleLabel.text is nil.
func handleButton (sender:UIButton) {
if let text = sender.titleLabel.text {
switch text {
case "1","2","3","4","5","6","7","8","9","0" :
println(sender.titleLabel.text)
default:
break
}
}
else {
// sender.titleLabel.text is nil
}
}