Home > Mobile >  Getting nil from Enum Swift
Getting nil from Enum Swift

Time:12-28

I have created a class

class NewTabContainerModal {

var newContainerDate = Date()
var newContainerSelectedFilter : NewAllErrorFIlter = .Yearly

func resetModal () {
    newContainerSelectedFilter = .Yearly
    newContainerDate = Date()
}
}

I have created an enum to get the values from it

enum NewAllErrorFIlter : String {
case Monthly = "2"
case Yearly = "1"
case Daily = "3"
}

Now in my ViewController class I have created a variable

var newContainerModal: NewTabContainerModal?

And in viewWillAppear I am trying to print the value of the enum like this

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated) 

 let newContainerModelData = newContainerModal?.newContainerSelectedFilter
 print(newContainerModelData)
}

but it is giving me nil instead of yearly value. I don't understand what I am doing wrong. Please help?

CodePudding user response:

Its because newContainerModal in your Viewcontroller is nil so newContainerModal?.newContainerSelectedFilter also gonna be a nil change it with

var newContainerModal: NewTabContainerModal = NewTabContainerModal()

In addition

let newContainerModelData = newContainerModal.newContainerSelectedFilter
print(newContainerModelData)

will print Yearly. to get that value use newContainerModelData.rawValue

  • Related