Swift - Cast Int into enum:Int

后端 未结 3 1900
悲&欢浪女
悲&欢浪女 2020-12-23 15:34

I am very new to Swift (got started this week) and I\'m migrating my app from Objective-C. I have basically the following code in Objective-C that works fine:



        
3条回答
  •  生来不讨喜
    2020-12-23 16:06

    elaborating on Jeffery Thomas's answer. to be safe place a guard statement unwrap the cast before using it, this will avoid crashes

       @IBAction func selectFilter(sender: AnyObject) {
         guard let filter = MyTimeFilter(rawValue: (sender as UIButton).tag) else { 
            return
        }
            timeFilterSelected = filter
         }
    

提交回复
热议问题