swift 用数组创建结构

xuo3flqw  于 2023-03-07  发布在  Swift
关注(0)|答案(2)|浏览(131)

开发人员好,我一直在尝试创建一个结构来改进代码
我有3个数组

var storeFuelGradesTypes: [String] = [""]
var storeFuelGradesPrice: [Double] = [0.0]
var storeFuelGradesID: [String] = [""]

当MapVc加载10次时填充数据(用于获取标记的API响应)

WalletService.shared.locateAndCheckinToNearestStore(toExplicitLocation: CLLocation(latitude: station.lat, longitude: station.long)) { storee, error in
  WalletService.shared.getSiteDetails { siteDetails, error in
     guard let fuelGrades = siteDetails?.fuelGrades else { return }
                                
     //this is how I fill the arrays getting the response from API and append the content from siteDetails and station response 
          let fuelType = fuelGrades.compactMap { $0.fuelType } // this is type [String]
          self?.storeFuelGradesTypes.append(contentsOf: fuelType)
          let priceGrades = fuelGrades.compactMap { $0.price } // this is type [Double]
          self?.storeFuelGradesPrice.append(contentsOf: priceGrades)
          let uriID = station.id. //this is type String
          self?.storeFuelGradesID.append(uriID)

在用户从选择标记的屏幕移动之后
我需要将信息传递到下一个屏幕这是我的第一个问题,因为我有

selectedStation?.id

这是用户选择的ID和站点信息(与API调用不同)

我找不到在数组上搜索的方法:

storeFuelGradesID

相同的ID,假设selectedStation?.id等于**- id:“第4111号决议”**
我在数组中存储的是:

(lldb) po storeFuelGradesPrice
▿ 10 elements
  - 0 : 1.339
  - 1 : 1.339
  - 2 : 1.339
  - 3 : 1.339
  - 4 : 1.339
  - 5 : 1.339
  - 6 : 1.339
  - 7 : 1.339
  - 8 : 1.339
  - 9 : 1.339

(lldb) po storeFuelGradesTypes
▿ 10 elements
  - 0 : "REGULAR"
  - 1 : "REGULAR"
  - 2 : "REGULAR"
  - 3 : "REGULAR"
  - 4 : "REGULAR"
  - 5 : "REGULAR"
  - 6 : "REGULAR"
  - 7 : "REGULAR"
  - 8 : "REGULAR"
  - 9 : "REGULAR"

(lldb) po storeFuelGradesID
▿ 10 elements
  - 0 : "4111"
  - 1 : “4222"
  - 2 : “4333"
  - 3 : “5333"
  - 4 : “7888"
  - 5 : “4666"
  - 6 : “2111"
  - 7 : “2222"
  - 8 : “3888"
  - 9 : “9111"

(lldb)

我想创建一个数组,用于存储3种类型的信息storeFuelGradesID + storeFuelGradesTypes + storeFuelGradesPrice
和我已经有了的ID,用for语句查找新数组的ID(包含所有信息)

swvgeqrz

swvgeqrz1#

您可以尝试这种方法,使用一个简单的模型和一些数组函数来获取选定的id和所需的信息(价格),例如:

// the model
struct StoreFuelGrades: Identifiable {
    var id: String
    var type: String
    var price: Double
}

 // for testing, an array of info from the API
 let fuelStore = [StoreFuelGrades(id: "1", type: "type-1", price: 1.0),
                  StoreFuelGrades(id: "2", type: "type-2", price: 2.0),
                  StoreFuelGrades(id: "3", type: "type-3", price: 3.0)]
 
 let selectedId = "2"
 // get the selectedId from the fuelStore
 if let selectedFuel = fuelStore.first(where: {$0.id == selectedId}) {
     print("---> selectedFuel: \(selectedFuel)")
 }
 
 // get all prices
 let prices = fuelStore.map{$0.price}
 print("---> prices: \(prices)")
gzjq41n4

gzjq41n42#

我真傻我知道答案了
我创造了这个结构

struct storeFuelGradesInformation {
        var fuelGradesID: String
        var fuelGradesType: [String]
        var fuelGradesPrice: [Double]
        
    }

    var fuelGradesTotalInformationStore: [storeFuelGradesInformation] = []

然后用API响应填充它:

var captureFuelGrades: storeFuelGradesInformation = storeFuelGradesInformation(fuelGradesID: uriID, fuelGradesType: fuelType, fuelGradesPrice: priceGrades)

并附加到燃料等级总信息存储器。

self?.fuelGradesTotalInformationStore.append(captureFuelGrades)

用户选择标记首选项后:

let idToGet = selectedStation?.id
                
                for arrayToFind in fuelGradesTotalInformationStore {
                    if arrayToFind.fuelGradesID == idToGet{
                        print(arrayToFind)//here I can pass the information safely 
                        return
                    }
                }

谢谢,如果有人有更好的主意,我愿意改进

相关问题