魔术记录无法为可转换类型属性插入数据

时间:2019-02-14 18:26:43

标签: ios swift core-data magicalrecord magicalrecord-2.2

在将代码库转换为Swift 4.2之后,为“二进制数据”类型的属性设置值时出现崩溃。我已将属性更新为“可转换类型”,但未在DB中插入数据。

数据类型为二进制数据时出错: “给定类型__NSDictionary错误的所需类型NSData”

{
    var modelValue: (value: Any?, defaultValue: Any?)
    if value is Array<Any>,
        let tempValue = value as? [String] {
        modelValue = (tempValue.joined(separator:","),
                      DBManagedObject.DefaultValues.defaultDateValue as AnyObject?)
    } else {
        switch attribute.attributeType {
        case .integer16AttributeType,
             .integer32AttributeType,
             .integer64AttributeType,
             .decimalAttributeType,
             .doubleAttributeType,
             .floatAttributeType:
            fallthrough

        case .booleanAttributeType:
            modelValue = numberValueForAttribute(value: value)

        case .stringAttributeType:
            modelValue = stringValueForAttribute(value: value)

        case .dateAttributeType:
            let defaultDateFormat = TBManagedObject.DefaultValues.defaultDateFormatString
            let dateFormat = (model as? ParserDateDelegate)?.dateFormatString ?? defaultDateFormat
            modelValue = dateValueForAttribute(value: value, dateFormat: dateFormat)

        default:
            modelValue = (value, nil)
        }
    }

    if modelValue.value != nil {
        model.setValue(modelValue.value, forKey: attribute.name)
    } else {
        if model.value(forKey: attribute.name) == nil {
            model.setValue(modelValue.defaultValue, forKey: attribute.name)
        }
    }
}

0 个答案:

没有答案