简体   繁体   中英

Getting nil from Enum Swift

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM