If not let - in Swift

前端 未结 3 952
情歌与酒
情歌与酒 2020-12-29 01:14

is there is a way to negate the \"if let\" in swift? This looks silly to me:

    if let type = json.type  {

    } else {
        XCTFail(\"There is no type         


        
3条回答
  •  爱一瞬间的悲伤
    2020-12-29 01:50

    Another alternative I've used a few times:

    switch json.type
    {
        case .None: // ...
        case .Some(.Object): // ...
        case .Some(.Array):  // ...
        case .Some(.Number): // ...
        case .Some(.String): // ...
    }
    

    Since the ? is actually Optional which is an enum on its own, defined as:

    enum Optional : Reflectable, NilLiteralConvertible 
    {
        case None
        case Some(T)
    
        ...
    }
    

提交回复
热议问题