美文网首页程序员
Swift: Dictionary+Extention(字典防护

Swift: Dictionary+Extention(字典防护

作者: 心猿意码_ | 来源:发表于2022-07-19 10:11 被阅读0次
import Foundation

extension Dictionary {
    ///字典的安全插入
    mutating func set(_ key : Dictionary.Key , _ value : Dictionary.Value?) {
        if let o = value {
            self[key] =  o
        } else {
            print("Warning! Dictionary:\(self) append an nil value key is '\(key)'")
        }
    }
    
    /// 字典安全取值
    func object(_ key : Dictionary.Key) -> Dictionary.Value? {
        let value = self[key]
        if value == nil {
            return nil
        }
        return value
    }
    
    func string(_ key : Dictionary.Key) -> String {
        let value = self[key]
        if value == nil {
            return ""
        }
        if let s = value as? String {
            return s
        }
        if let i = value as? Int {
            return i.description
        }
        if let d = value as? Double {
            return d.description
        }
        return ""
    }
    
    func int(_ key : Dictionary.Key) -> Int {
        let value = self[key]
        if value == nil {
            return 0
        }
        if let s = value as? String {
            
            if let n = Int(s) {
                return n
            }else {
                print("Warning! Dictionary:\(self) int for key:'\(key)' value:'\(s)' is not cover to Int!")
                return 0
            }

        }
        if let i = value as? Int {
            return i
        }
        if let d = value as? Double {
            return Int(d)
        }
        return 0
    }
    
    func double(_ key : Dictionary.Key) -> Double {
        let value = self[key]
        if value == nil {
            return 0.0
        }
        if let s = value as? String {
            if let n = Double(s) {
                return n
            }else {
                print("Warning! Dictionary:\(self) int for key:'\(key)' value:'\(s)' is not cover to Double!")
                return 0
            }
        }
        if let i = value as? Int {
            return Double(i)
        }
        if let d = value as? Double {
            return d
        }
        return 0.0
    }
    // 处理字典中所有空值
    func scrollDic(dic : Dictionary<String,Any>) -> Dictionary<String,Any> {
        var tempDic : Dictionary<String,Any> = dic
        for item in tempDic.keys {
            if(tempDic[item] == nil || (tempDic[item] as? NSNull) != nil ){
                tempDic[item] = ""
            }
            if((tempDic[item] as? Dictionary<String,Any>) != nil ){
                let getDic = scrollDic(dic: tempDic[item] as! Dictionary<String,Any>)
                tempDic[item] = getDic
            }
            if((tempDic[item] as? [Dictionary<String,Any>]) != nil ){
                let tempArr : [Dictionary<String,Any>] = (tempDic[item] as! [Dictionary<String,Any>])
                var tempNArr : [Dictionary<String,Any>]? = []
                tempNArr?.removeAll()
                for i in tempArr{
                    let iDic = scrollDic(dic: i )
                    tempNArr?.append(iDic)
                }
                tempDic[item] = tempNArr
            }
        }
        return tempDic
    }
}

相关文章

网友评论

    本文标题:Swift: Dictionary+Extention(字典防护

    本文链接:https://www.haomeiwen.com/subject/igmjirtx.html