Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

字典转换model nil变成了"nil" #71

Open
MuRanJiangXia opened this issue Sep 26, 2024 · 1 comment
Open

字典转换model nil变成了"nil" #71

MuRanJiangXia opened this issue Sep 26, 2024 · 1 comment

Comments

@MuRanJiangXia
Copy link

class PublishSubmitModel: Convertible {
var title = ""

var content = ""

required init() {}

}

// 测试
let dic = [
"title": "",
"content": nil
]

    let model = dic.kj.model(PublishSubmitModel.self)
    
    Logger.info("content: \(model.content)")

model中的content 变为“nil” 字符串了

@MuRanJiangXia
Copy link
Author


func kj_modelValue(from jsonValue: Any?, _ property: Property) -> Any? {
方法中打印
Printing description of jsonValue:
▿ Optional

  • some : nil

    func kj_modelValue(from jsonValue: Any?, _ property: Property) -> Any? {

    guard jsonValue != nil, property.dataType == String.self else {
        return jsonValue
    }
    
    if let newJson = jsonValue as? Bool {
        return newJson == true ? "1" : "0"
    }
    
    if let newJson = jsonValue as? NSNumber {
        return newJson
    }
    
    guard let newJson = jsonValue as? String else {
        // 处理 string nil ;Optional<Any> - some : nil
        return ""
    }
    return newJson
    

    }
    做了一下处理能规避一下 不知道还有好点的方法不

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant