You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何根据API返回数据设置UISwitch状态?能否用循环替代索引?

问题:通过循环遍历API数据设置UISwitch状态而非使用索引

场景说明

我有一个包含两个UISwitch按钮的界面,API返回的数据如下:

"CustomerSubscribed": [
        {
            "Customer_ID": "100132",
            "Mobile": "3213103428",
            "Subscription": "Bill Payment",
            "Mnemonic": "BILL",
            "IsActive": 0
        },
        {
            "Customer_ID": "100132",
            "Mobile": "3213103428",
            "Subscription": "Funds Transfer",
            "Mnemonic": "IBFT",
            "IsActive": 1
        }
    ] 

两个UISwitch的定义代码如下:

private var fundSwitch: UISwitch = {
    let switchView = UISwitch()
    switchView.isOn = false
    switchView.onImage = UIImage(named: "switchOn")
    switchView.offImage = UIImage(named: "switchOff")
    switchView.onTintColor = UIColor.purple
    switchView.tag = 1
    switchView.addTarget(self, action: #selector(switchStateDidChange), for: .valueChanged)
    switchView.translatesAutoresizingMaskIntoConstraints = false
    return switchView
}()

private var billSwitch: UISwitch = {
    let switchView = UISwitch()
    switchView.isOn = false
    switchView.onImage = UIImage(named: "switchOn")
    switchView.offImage = UIImage(named: "switchOff")
    switchView.onTintColor = UIColor.purple
    switchView.tag = 2
    switchView.addTarget(self, action: #selector(switchStateDidChange), for: .valueChanged)
    switchView.translatesAutoresizingMaskIntoConstraints = false
    return switchView
}()

需求:根据IsActive字段设置UISwitch的状态——IsActive为1时开关开启,为0时关闭。

当前尝试的两种实现方式

方式一

@objc func switchStateDidChange(_ sender: UISwitch) {
     if sender.tag == Switch.fundTrasnfer.rawValue {
            print( sender.isOn ? customerSubscribe?[0].isActive ?? false: "Fund Transfer Switch is Off" )
        } else {
            print( sender.isOn ? customerSubscribe?[1].isActive ?? false: "Bill Payment Fund Transfer Switch is Off" )
        }
}

方式二

@objc func switchStateDidChange(_ sender: UISwitch) {
     for customer in self.customerSubscribe! {
            if customer.isActive == true {
                print(sender.isOn ? "Switch is ON": "Switch is Off")
            } else {
                print(sender.isOn ? "Switch is ON": "Switch is Off")
            }
        }
}

架构与数据接收

采用VIP架构,在以下方法中接收API响应:

extension SubscriptionViewController: SubscribeViewProtocol {
    func saveSuccessCustomerData(response: CustomerSubscribeEntity.CustomerServiceResponse) {
        
    }
    
    func successSetupSubscription(response: SubscriptionEntity.SubscriptionServicesResponse) {
        DispatchQueue.main.async {
            print("Id \(response.customerSubscribed?[0].customerID)")
            self.customerSubscribe = response.customerSubscribed
            
            self.fundTransferLbl.text = self.customerSubscribe?[0].subscription
            
            self.billPaymentLbl.text = self.customerSubscribe?[1].subscription
     
        }
    }
}

用于存储响应数据的变量定义:

var customerSubscribe: [SubscriptionEntity.CustomerSubscribed]?

界面截图

界面截图

UI为纯代码实现,两个UISwitch已正确定义。

核心问题

能否通过循环遍历数据来设置开关状态,而非直接使用索引?


内容的提问来源于stack exchange,提问作者Ahmed Saeed

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 04:35:36