我需要更改对象数组中的值
从API接收的选定运动的数组
arrSelectedID = [["slug": cricket,
"id": 1,
"banner_image": https://_1558964043.jpg,
"name": Cricket,
"icons": {
black = "https://sport_icon_cricket_black.png";
green = "https://sport_icon_cricket_green.png";
grey = "https://sport_icon_cricket_gray.png";
white = "https://sport_icon_cricket_white.png";
}],
["slug": soccer,
"banner_image": https://1558964051.jpg
"icons": {
black = "https://sport_icon_soccer_black.png";
green = "https://sport_icon_soccer_green.png";
grey = "https://sport_icon_soccer_gray.png";
white = "https://sport_icon_soccer_white.png";
},
"id": 2,
"name": Soccer]]
我的阵列
struct ObjSportsList:Codable {
var id:Int
var name:String
var slug:String
var selected:Bool?
var icons:ObjSportsIcon
}
struct ObjSportsIcon:Codable {
var green:String
var grey:String
var white:String
var black:String
}
var arrSports:[ObjSportsList] = [] // Array which is custom object
以下响应显示我的自定义对象“ ObjSportsList”
arrSports = (id: 1,
name: "Cricket",
slug: "cricket",
selected: nil,
),
(id: 2,
name: "Soccer",
slug: "soccer",
selected: nil,
),
(id: 3,
name: "Baseball",
slug: "baseball",
selected: nil,
),
我想更改“选定的”值= true,这是从“数据”中的API数组获取的ID示例:[1,3]仅更改第1和第3个selected
值== true
我尝试使用以下代码,但数组未更新
for (index, var sports) in self.arrSports.enumerated() {
for selectedSports in arrSelectedID {
if selectedSports["id"] as! Int == sports.id {
sports.selected = true
}
}
答案 0 :(得分:0)
请参考以下代码替换对象import UIKit
var pastData = [
"What was the greatest achievement you accomplished the past five years?",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10",
]
var futureData = [
"What is the single most important thing to have accomplished within the next five years?",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10",
]
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func pastButton(_ sender: UIButton) {
}
@IBAction func futureButton(_ sender: UIButton) {
}
@IBOutlet weak var label: UILabel!
}
中的特定值
Array
答案 1 :(得分:0)
我使用自己的代码解决了该问题,请检查此代码
for (index, sports) in self.arrSports.enumerated() {
for selectedSports in arrSelectedID {
if selectedSports["id"] as! Int == sports.id {
self.arrSports[index].selected = true
}
}
}