变更记录

序号 录入时间 录入人 备注
1 2015-04-13 Alfred Jiang -
2 2015-12-23 Alfred Jiang -

方案名称

语法 - If not let - in Swift

关键字

语法 \ optional \ ? \ ! \ if let

需求场景

  1. 判断 optional 字段是否有效\无效时

参考链接

  1. Stacck Overflow - If not let in Swift

详细内容

正常的 if let 用法

if let type = json.type  {

} else {
    //There is no type in the root element
}

if not let 实现

func ifNotLet<T>(optional: T?, closure: @autoclosure () -> ()) -> T {
    switch optional {
    case .None:
        closure()
    case let .Some(value)
        return value
    }
}

let type = ifNotLet(json.type) {
    XCTFail("There is no type in the root element")
}

效果图

(无)

备注

(无)

results matching ""

    No results matching ""