ios - 如何快速替换字典数组中的值?

标签 ios swift

我有字典数组,我试图将其存储在 NSuserdefault 中,但因为它包含 <null>应用程序崩溃,我们可以更换 <null>和 ””? 有什么办法可以解决这个问题吗?

这是我的字典数组:

var array = (
                            {
                    ClinicID = "<null>";
                    "Patient_SurveyID" = 1956;
                    "Patient_Treatment_PlanID" = "<null>";
                    PhysicianID = "<null>";
                },
                            {
                    ClinicID = "<null>";
                    "Patient_SurveyID" = 1956;
                    "Patient_Treatment_PlanID" = "<null>";
                    PhysicianID = "<null>";
                },
    )

最佳答案

所以你有一个字典数组

let list: [[String:Any]] = []

每个字典的类型都是[String:Any]

这是将 NSNull 值替换到字典中的代码 “”

let list: [[String:Any] = []

let updatedDict = list.map { (dict) -> [String:Any] in
    let keysWithEmptStringValue = dict.filter { $0.1 is NSNull }.map { $0.0 }
    var dict = dict
    for key in keysWithEmptStringValue {
        dict[key] = ""
    }
    return dict
}

从 NSMutableArray 到 [[String:Any]]

要将 NSMutableArray 转换为 Swift 通用数组,请尝试此代码

let array = NSMutableArray()
let list: [[String:Any]] = array.flatMap { $0 as? [String:Any] }

关于ios - 如何快速替换字典数组中的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38456966/

相关文章:

ios - NSCFCalendar dateFromComponents : components cannot be nil

Swift 3 嵌套函数与闭包

ios - 一对多关系的 NSBatchUpdateRequest

swift - 如何在 Xcode Playground 的右侧栏中创建我的类型的自定义表示?

无法在 Assets 目录中读取 Swift 图像

iphone - UIScrollView contentOffsets 是绝对的吗?

iphone - iPad 的横向应用程序没有主屏幕横向

objective-c - 快速重新加载 UITableView 数据 (iOS)

html - 为什么 Gmail 会忽略我的媒体查询? (在 iOS 上)

ios - 如何在 let 命令中添加先前声明的变量