开发人员好,我一直在尝试创建一个结构来改进代码
我有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(包含所有信息)
2条答案
按热度按时间swvgeqrz1#
您可以尝试这种方法,使用一个简单的模型和一些数组函数来获取选定的id和所需的信息(价格),例如:
gzjq41n42#
我真傻我知道答案了
我创造了这个结构
然后用API响应填充它:
并附加到燃料等级总信息存储器。
用户选择标记首选项后:
谢谢,如果有人有更好的主意,我愿意改进