-
Notifications
You must be signed in to change notification settings - Fork 1k
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
check the value for parse #1119
Comments
Hi, I may have a solution for this, as I understood you want that if there's a null in the name it should return nothing. [IF]: user and url are Any types then if you make an optional downcast to both of them you can determine that url could be a String because it has quotes but on the other side name can´t be a string so it assigns to nil. [?? Array]: Assign an empty array struct Repo: Mappable {
var name: String?
var url: String?
init?(map: Map) {
if(map.JSON["name"] as? String == nil) {
return nil
}
name <- map["name"]
url <- map["url"]
}
}
let repo = Mapper<Repo>().mapArray(JSONString: jsonFile) ?? Array() Result: [
{
"user":null,
"url":"https://github.com/Hearst-DD/ObjectMapper1"
},
{
"user":"Swiftter",
"url":"https://github.com/Hearst-DD/ObjectMapper2"
}
] Swift Output: repo = [GitHubIssue.Repo(name: Optional("Swiftter"), url: Optional("https://github.com/Hearst-DD/ObjectMapper2"))]
//Or with your JSON file
repo = [] Let me know if I helped you or if I didn't understand ;) |
Thanks for your replay. That's my wanna effect, The optional is the universal way by me, but it's not well for used where the optional need to work with |
Your JSON dictionary:
Your model:
What you did:
What you expected:
I exepected something like:
but now,
it's parse object success, when somewhere user this required property, the app crashed!
I wanna let the property required not optional, how I can achieve that effect ?
like
init?(map: Map) { }
to check thekey
is exist for check the value ?The text was updated successfully, but these errors were encountered: