-1

我希望用户选择该城市的城市和地区。当我选择城市时,我想在选择器视图中列出该城市的地区。我还想在文本中显示城市的选择和地区的选择。

struct locationView: View {


@State var selectedFrameworkIndex = 0
@State var tap1 : Bool = false

@ObservedObject var cityfetch = cityFetcher()

var selectedCityName: String? {
    if !cityfetch.cities.isEmpty && selectedFrameworkIndex < cityfetch.cities.count {
        return cityfetch.cities[selectedFrameworkIndex].city_name
    } else {
        return nil
    }
}

var body: some View {
    
          
            VStack{
                
                
                Picker(selection: $selectedFrameworkIndex, label: Text("")) {
                    ForEach(0 ..< cityfetch.cities.count, id: \.self) {
                        
                        Text(self.cityfetch.cities[$0].city_name)
                    }
                }.padding(.trailing, 50)
                    .id(UUID())
                
                
                Picker(selection: $selectedFrameworkIndex, label: Text("")){
                    ForEach(0 ..< cityfetch.cities.count, id: \.self) {
                        
                        Text(self.cityfetch.cities[$0].district_name)
                    }
                }.padding(.trailing, 50)
                    .id(UUID())
                
            }
            
            
            
            Text("Seçiminiz: \(selectedCityName ?? "")")
                .font(.system(size: 20, weight: .regular, design: .rounded))
            
            
        }

struct city : Decodable, Identifiable{

let id = UUID()
let city_id : Int
let city_name : String
let district_id : Int
let district_name : String
}

我的班级正在跟随..

class cityFetcher : ObservableObject{

@Published var cities = [city]()


init() {
    loadCity()
    
}
func loadCity(){
    
    let url = URL(string: "https://raw.githubusercontent.com/midorikocak/turkish-cities-districts/master/data/il-ilce.json")
    URLSession.shared.dataTask(with: url!) { (data, response, error) in
        do{
            let cities = try JSONDecoder().decode([city].self, from: data!)
            DispatchQueue.main.async {
                self.cities = cities
                
            }
                
            
        }   catch{
            print("Error")
        }
    }.resume()
    
    
}

}

我还将我的 JSON 信息发布为图像。

在此处输入图像描述

4

1 回答 1

0

添加函数getUniqiueCities()cityFetcher创建一个独特的城市数组,并在之后调用这个函数self.cities = cities。在. @Published var uniqueCities : [city] = []_cityFetcher

func getUniqiueCities() {
        var uniqueCities: [city] = []
        for city in self.cities {
            let isExists =  uniqueCities.contains{ item in
                return item.city_name == city.city_name
            }
            if !isExists {
              uniqueCities.append(city)
            }
        }
        self.uniqueCities =  uniqueCities
    }

unique cities根据唯一的城市名称运行循环并显示区域。LocationView使用以下代码更新

struct locationView: View {
    @State var selectedFrameworkIndex = 0
    @State var selectedCityIndex = 0
    @State var tap1 : Bool = false
    @ObservedObject var cityfetch = cityFetcher()

    var selectedCityName: String? {
        if !self.cityfetch.uniqueCities.isEmpty && selectedFrameworkIndex < self.cityfetch.uniqueCities.count {
            return self.cityfetch.uniqueCities[selectedFrameworkIndex].city_name
        } else {
            return nil
        }
    }

    var body: some View {
        VStack{
            Picker(selection: $selectedFrameworkIndex, label: Text("")) {
                ForEach(0 ..< self.cityfetch.uniqueCities.count, id: \.self) {
                    Text(self.cityfetch.uniqueCities[$0].city_name)
                }
            }.padding(.trailing, 50)
            .id(UUID())
            Picker(selection: $selectedCityIndex, label: Text("")){
                ForEach(0 ..< self.getFilteredDistricts().count, id: \.self) {
                    Text(self.getFilteredDistricts()[$0].district_name)
                }
            }.padding(.trailing, 50)
            .id(UUID())
            
        
                  
        Text("Seçiminiz: \(selectedCityName ?? "")")
            .font(.system(size: 20, weight: .regular, design: .rounded))
        }
    }
    
    func getFilteredDistricts() -> [city] {
        return self.cityfetch.cities.filter({$0.city_name == self.cityfetch.uniqueCities[selectedFrameworkIndex].city_name})
    }
    
}

更新

根据评论,添加代码以获取 selectedDistrict:

var selectedDistrict: String? {
        if !self.getFilteredDistricts().isEmpty && selectedCityIndex < self.getFilteredDistricts().count {
            return self.getFilteredDistricts()[selectedCityIndex].district_name
        } else {
            return nil
        }
    }
于 2020-08-14T13:43:21.197 回答