SWIFT中具有两个条件的条件问题

时间:2019-05-23 02:17:18

标签: ios swift xcode

我不确定如何编写一个有两个条件但有一个简洁条件的条件语句。

我对带有一个条件的条件语句做得很好,但是在那之后,我无法执行第二个条件。

  @IBAction func sortButtonPressed(_ sender: UIBarButtonItem) {

        let fetchOptions: PHFetchOptions = PHFetchOptions()

        if (self.sortButton.title == "In the past") {
            fetchOptions.sortDescriptors = [NSSortDescriptor(key: "modificationDate",
                                                             ascending: false)]
            self.fetchResult = PHAsset.fetchAssets(in: assetCollection!, options: fetchOptions )
            self.sortButton.title = "The latest"
        }
    }

在上面的代码中,如果您点击名为“过去”的“ sortButtonPressed”,则最新的照片将按照过去的顺序进行更新。但是我想做的是再次点击“最新”按钮,以最新的顺序更新照片。

4 个答案:

答案 0 :(得分:0)

let fetchOptions: PHFetchOptions = PHFetchOptions()
var isDescending = self.sortButton.title == "In the past"
var nextTitle = self.sortButton.title == "In the past" ? "The latest" : "In the past"

fetchOptions.sortDescriptors = [NSSortDescriptor(key: "modificationDate",
                                                 ascending: isDescending)]
self.fetchResult = PHAsset.fetchAssets(in: assetCollection!, options: fetchOptions )
self.sortButton.title = nextTitle

答案 1 :(得分:-1)

尝试使用三元运算符 https://syntaxdb.com/ref/swift/ternary

@IBAction func sortButtonPressed(_ sender: UIBarButtonItem) {

    let fetchOptions: PHFetchOptions = PHFetchOptions()

    fetchOptions.sortDescriptors = [NSSortDescriptor(key: "modificationDate",
                                    ascending: sender.title == "In the past" ? false : true)]
    self.fetchResult = PHAsset.fetchAssets(in: assetCollection!, options: fetchOptions )
    self.sortButton.title = "The latest"
}

答案 2 :(得分:-1)

@IBAction func sortButtonPressed(_ sender: UIBarButtonItem) {

    let fetchOptions: PHFetchOptions = PHFetchOptions()
    let newTitle: String
    let ascending: Bool

    if (self.sortButton.title == "In the past") {
        newTitle = "The latest"
        ascending = false
    } else {
        newTitle = "In the past"
        ascending = true
    }

    fetchOptions.sortDescriptors = [NSSortDescriptor(key: "modificationDate",
                                                         ascending: ascending)]
    self.fetchResult = PHAsset.fetchAssets(in: assetCollection!, options: fetchOptions )
    self.sortButton.title = newTitle
}

最好将"In the past""The latest"分配给常量或枚举大小写。

答案 3 :(得分:-1)

您可以对多个条件使用switch语句,这是我理解switch语句的代码。您也可以使用带有单独常量的字符串。因此,条件不会错配。

   let STR_LATEST = "In the past"
   let STR_PAST = "The latest"

    @IBAction func sortButtonPressed(_ sender: UIBarButtonItem) {

        switch sender.title {
        case STR_LATEST: buttonTapped(isForLatest: true)
        case STR_PAST: buttonTapped(isForLatest: false)
        default:
            print("Nothing")
        }

    }

    func buttonTapped(isForLatest: Bool) {

        let fetchOptions: PHFetchOptions = PHFetchOptions()

        if isForLatest {

            fetchOptions.sortDescriptors = [NSSortDescriptor(key: "modificationDate",
                                                             ascending: true)]
            self.fetchResult = PHAsset.fetchAssets(in: assetCollection!, options: fetchOptions )
            self.sortButton.title = STR_PAST

        } else {

            fetchOptions.sortDescriptors = [NSSortDescriptor(key: "modificationDate",
                                                             ascending: false)]
            self.fetchResult = PHAsset.fetchAssets(in: assetCollection!, options: fetchOptions )
            self.sortButton.title = STR_LATEST

        }
    }