【问题标题】:I want to change my label colour according to text in response from server我想根据服务器响应的文本更改标签颜色
【发布时间】:2018-09-13 17:44:56
【问题描述】:

我在 CellForRowat Indexpath 中使用 For 循环来更改颜色,但它显示错误,我该如何解决它。我输入了正确的键值

错误(由于未捕获的异常而终止应用程序 'NSInvalidArgumentException',原因:'-[_SwiftValue objectForKey:]: 无法识别的选择器发送到实例 0x6040000f6680'

for all in arrayForType!
{
    let type = (all as AnyObject).object(forKey: "type") as! String
    print(type)

    if type == "Send"
    {
        cell.lblForSend.backgroundColor = UIColor.red
    }
    else
    {
        cell.lblForSend.backgroundColor = UIColor.green
    }    
}

【问题讨论】:

  • 哪里出错了
  • 可以在tableView(_tableView: UITableView, cellForRowAt indexPath: IndexPath)方法中设置。在这个方法里面可以设置条件,可以设置背景颜色。
  • 你确定问题出在那个for循环上吗?

标签: ios swift uitableview uilabel


【解决方案1】:

我已经解决了问题,这是正确的方法。

let type = (arrayForType[indexPath.row] as AnyObject).object(forKey: "type") as! String
print(type)

if type == "Send"
{
    cell.lblForSend.backgroundColor = UIColor.red
}
else
{
    cell.lblForSend.backgroundColor = UIColor.green
}    

【讨论】:

    【解决方案2】:

    您应该始终尽量避免隐式展开,尤其是当您处理从服务器获取的数据时。尝试使用 Optional Binding 来实现更安全。就这样

    guard let arrayForType = arrayForType as? [NSDictionary] else { return }
    for all in arrayForType {
        if let type = all.object(forKey: "type") as? String, type == "Send" {
            cell.lblForSend.backgroundColor = .red
        } else {
            cell.lblForSend.backgroundColor = .green
        }
    }
    

    【讨论】:

    • 错误信息与强制解包 nil 值无关
    • 错误信息是关于这一行的无效参数:let type = (all as AnyObject).object(forKey: "type") as!字符串可选绑定避免崩溃......
    • @JabaOdishelashvili 错误消息清楚地写着unrecognized selector sent to instance。如果解包 nil 是问题所在,错误会显示为 Unexpectedly found nil while unwrapping an Optional value
    【解决方案3】:

    错误似乎在这一行:

    let type = (all as AnyObject).object(forKey: "type") as! String
    

    您将all 视为AnyObject(顺便说一句,这是多余的),但是您调用它的方法-object(forKey: String)-是NSDictionary 类的方法

    不如试试

    guard let array = arrayForType else { return }
    for all in array
    {
        guard let dict = all as? NSDictionary else { continue }
        guard let type = dict.object(forKey: "type") as? String else { continue }
    
        print(type)
    
        if type == "Send"
        {
            cell.lblForSend.backgroundColor = UIColor.red
        }
        else
        {
            cell.lblForSend.backgroundColor = UIColor.green
        }    
    }
    

    此外,请避免强制解包选项(! 运算符),尽管这不是您的情况导致崩溃的原因

    【讨论】:

    • 你为什么要降低我的回答?你的答案是一样的...... :))
    • @JabaOdishelashvili 您的回答与实际问题无关,即在AnyObject 上调用NSDictionary 方法。
    • 但是我已经用安全的方式写出来了...你的答案是一样的...除了你之前转换成字典...
    【解决方案4】:

    您的问题是AnyObject 没有.object(forKey: #"") 方法,objectForKey 方法是NSDictionary 的方法,这是您崩溃的原因

    https://developer.apple.com/documentation/foundation/nsdictionary/1414347-objectforkey

    【讨论】:

      猜你喜欢
      • 2018-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-14
      • 1970-01-01
      相关资源
      最近更新 更多