Swift 处理多个可选变量?

Swift 处理多个可选变量?,swift,Swift,我正在努力处理代码中的多个选项以及相应的错误处理 请看下面的例子 func getCoordinates1(pLatitude: Double?, pLongitude: Double?) -> CLLocationCoordinate2D?{ var coord:CLLocationCoordinate2D? if let latitude = pLatitude { if let longitude = pLongitude {

我正在努力处理代码中的多个选项以及相应的错误处理

请看下面的例子

func getCoordinates1(pLatitude: Double?, pLongitude: Double?) -> CLLocationCoordinate2D?{
    var coord:CLLocationCoordinate2D?

    if let latitude = pLatitude {
        if let longitude = pLongitude {
            coord = CLLocationCoordinate2DMake(lat, long)
        }
    }

    return coord
}
这看起来不错,但在现实世界中,您可能需要一些错误处理,在这里,我正在寻找一种不需要重复代码的好方法:

func getCoordinates2(pLatitude: Double?, pLongitude: Double?) -> CLLocationCoordinate2D? {
    var coord:CLLocationCoordinate2D?

    if let latitude = pLatitude {
        if let longitude = pLongitude {
            coord = CLLocationCoordinate2DMake(latitude, longitude)
        } else {
            // do something to catch the error
        }
    } else {
        // do the same as above (duplicate code)
    }

    return coord
}
我有时会使用布尔值来跟踪它:

func getCoordinates3(pLatitude: Double?, pLongitude: Double?) -> CLLocationCoordinate2D? {
    var coord:CLLocationCoordinate2D?
    var success = false

    if let latitude = pLatitude {
        if let longitude = pLongitude {
            coord = CLLocationCoordinate2DMake(latitude, longitude)
            success = true
        }
    }
    if !success {
        // do something to catch the error
    }
    return coord
}
或者我使用提前退出的模式,但我认为这也是错误的

func getCoordinates4(pLatitude: Double?, pLongitude: Double?) -> CLLocationCoordinate2D? {

    if let latitude = pLatitude {
        if let longitude = pLongitude {
            return CLLocationCoordinate2DMake(latitude, longitude)
        }
    }
    // do something to catch the error

    return nil
}

当然,这是一个只包含两个选项的分条示例,但在解析json时,可能需要更多的级联。我希望想法和问题都很清楚。

如评论中所述,在Swift 1.2中,您可以做到:

func getCoordinates2(pLatitude: Double?, pLongitude: Double?) -> CLLocationCoordinate2D? {
    var coord:CLLocationCoordinate2D?

    if let latitude = pLatitude, longitude = pLongitude {
        coord = CLLocationCoordinate2DMake(latitude, longitude)
    } else {
        // do something to catch the error
    }

    return coord
}
我建议对代码进行结构化,以便在代码可用时更容易切换到该样式

试试这个:

func getCoordinates2(pLatitude: Double?, pLongitude: Double?) -> CLLocationCoordinate2D? {
    var coord:CLLocationCoordinate2D?

    if pLatitude != nil && pLongitude != nil {
        let latitude = pLatitude!
        let longitude = pLongitude!
        coord = CLLocationCoordinate2DMake(latitude, longitude)
    } else {
        // do something to catch the error
    }

    return coord
}

这与Swift 1.2版本具有相同的语义和结构,当它可用时,您可以切换到较新的语法,而无需更改任何缩进。

基本上,您尝试执行的操作可以分为两组操作:

  • 第一个是数据的简单处理(有效负载)
  • 第二是处理潜在的错误条件

对错误条件的处理总是相同的,即检查前一个操作是否返回错误并将其传递到上游,如果到目前为止一切正常,则处理结果并进一步传递成功。这可以很好地封装在一个函数中,该函数将result和closure作为输入并返回另一个结果。我认为罗伯·纳皮尔的作品会对你非常有用。

这就是你想要的吗?是的,但它将在Swift 1.2中提供(目前仅为测试版),仅供参考,你试图避免的模式被称为“末日金字塔”。谢谢你的代码。我不想这么做,因为我觉得选择期权的想法是为了避免这种零测试。但我想我会坚持到1.2