如何使快速代码更好地访问数组?

问题描述 投票:0回答:1

我有以下代码

  if let unwrappedDict = snapshot.value as? NSDictionary {
                for (index, dd) in unwrappedDict {
                    let dd = dd as? NSDictionary ?? [:]
                    id = dd["id"] as? String ?? ""
                    let ccode = dd["code"] as? String ?? ""
                    if (ccode == code) {
                        if id.count > 0 {
                            found = true;
                        }
                    }
                }
            }

如何改善此代码?我特别在谈论这一行,让dd = dd为? NSDictionary ?? [:]?

ios arrays swift nsdictionary
1个回答
0
投票

可能会有其他改进,但是我会这样做。

if let unwrappedDict = snapshot.value as? NSDictionary {
    for item in unwrappedDict {
        if let internalDict = item as? NSDictionaty {
            if let id = internalDict["id"] as? String, let ccode = internalDict["code"] as? String {
                if ccode == code && !id.isEmpty {
                    found = true
                    break
                }
            }
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.