簡體   English   中英

如何從 SwiftUI 中的 function 返回枚舉?

[英]How to return an enum from a function in SwiftUI?

我想從我的 function 返回一個enum 為此,我需要對返回類型進行分類。

但是,如果我想返回enum ,返回類型是什么?

我的代碼如下:

class Timezone {
    
    func getTimeZoneFromCountry(country: String) -> ??? {
        switch country {
        case "AU":
            return Timezone.AU
        case "US":
            return Timezone.US
        default:
            return Timezone.AU
        }
    }
   
    enum AU: String, Identifiable, CaseIterable {
        case AEST = "AEST"
        case AWST = "AWST"
        case ACST = "ACST"
        
        var id: AU {self}
    }
    
    enum US: String, Identifiable, CaseIterable {
        case CST = "CST"
        case EST = "EST"
        case MST = "MST"
        case PST = "PST"
        
        var id: US {self}
    }

}

我正在嘗試動態填充Picker

struct TimeZonePicker: View {
    @Binding var country: String // AU or US
    
    var body: some View {
        Picker("Timezone", selection: $country) {
            ForEach(*Dynamic enum here*) { i in
                Text(String(i.rawValue))
            }
        }
    }
}

編輯:@Sweeper 動態解決方案的新視圖:

var body: some View {
    Picker("Timezone", selection: $country) {
        ForEach(Timezone.getTimeZoneFromCountry(country: country)) { i in
            Text(String(i.rawValue))
        }
    }
}

使用單個枚舉,並讓getTimeZoneFromCountry僅返回枚舉的某些情況:

enum Timezone: String, Identifiable, CaseIterable {
    var id: Timezone { self }
    
    static func getTimeZoneFromCountry(country: String) -> [Timezone] {
        switch country {
        case "AU":
            return Timezone.AU
        case "US":
            return Timezone.US
        default:
            return Timezone.AU
        }
    }
    case AEST = "AEST"
    case AWST = "AWST"
    case ACST = "ACST"

    case CST = "CST"
    case EST = "EST"
    case MST = "MST"
    case PST = "PST"
    
    static let AU: [Timezone] = [.AEST, .AWST, .ACST]
    static let US: [Timezone] = [.CST, .EST, .MST, .PST]

}

接受的答案很合適,但對於那些關心代碼質量的人來說,這些是額外的:

首先

當原始值和大小寫完全相同時,您不需要為基於Stringenum定義原始值! 所以

case AEST = "AEST"

完全等於:

case AEST

其次:

您可以擴展Identifiable本身以實現所有RawRepresentable s:

extension Identifiable where Self: RawRepresentable {
    var id: Self { self }
}

第三點:

您可以在枚舉中使用關聯值來分割值,例如:

enum Timezone {
    case au([AU])
    case us([US])
,,,
}

首先:

嘗試通過避免默認分配來避免未知行為。 你可以有一個可選的 init 代替:

init?(raw: String) {
    switch raw.lowercased() {
    case "au": self = .au(AU.allCases)
    case "us": self = .us(US.allCases)
    default: return nil
    }
}

第五點:

不要忘記字符串是區分大小寫的。 嘗試處理靈敏度。 例如檢查lowercased


所以完整的代碼是這樣的:

enum Timezone {
    init?(raw: String) {
        switch raw.lowercased() {
        case "au": self = .au(AU.allCases)
        case "us": self = .us(US.allCases)
        default: return nil
        }
    }

    case au([AU])
    case us([US])

    enum AU: String, Identifiable, CaseIterable {
        case AEST
        case AWST
        case ACST
    }

    enum US: String, Identifiable, CaseIterable {
        case CST
        case EST
        case MST
        case PST
    }
}

extension Identifiable where Self: RawRepresentable {
    var id: Self { self }
}

將返回類型指定為字符串數組,並在 switch case 中從枚舉的 allCase 中返回字符串數組。

class Timezone {
    
    func getTimeZoneFromCountry(country: String) -> [String] {
    switch country {
    case "AU":
        return Timezone.AU.allCases.map({$0.rawValue})
    case "US":
    return Timezone.US.allCases.map({$0.rawValue})
    default:
    return Timezone.AU.allCases.map({$0.rawValue})
    }
    }
    
    enum AU: String, Identifiable, CaseIterable {
        case AEST = "AEST"
        case AWST = "AWST"
        case ACST = "ACST"
        
        var id: AU {self}
    }
    
    enum US: String, Identifiable, CaseIterable {
        case CST = "CST"
        case EST = "EST"
        case MST = "MST"
        case PST = "PST"
        
        var id: US {self}
    }
    
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM