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
Added methods that throw errors if the type is not what is expected. #487
base: master
Are you sure you want to change the base?
Conversation
I'm strongly in favor if this, I think it's a great addition. What would you say to removing "required" from the method names? It would fit with the existing SwiftyJSON naming conventions. Also, I think the type signature ( |
I would love to use this! |
@jkubicek I think this is confusing since we already have string property. |
I think it's better to use the |
And |
Looks interesting, can you kindly add a bit of test and update the syntax to swift3. Also, sorry for not getting to this sooner. |
@zhigang1992 Looking at the code, it seems that we're gonna end up with a confusing API. For instance, to unpack a string you have
I would argue that we should be as strict as possible and leave the responsibility on the user to fallback on whatever is best if the value cannot be unpacked. This is why I'm proposing this unique interface:
It covers all the cases above:
It also has the value to provide an explicit error if the unpack fail, as opposed to returning However it does introduce a code-breaking change, probably worth a SwiftyJSON v4 or something. What do you think? |
@ldiqual This change is huge, but the benefit is not that much. I don't think the try syntax is very beginner friendly or easy to use. |
I must admit I all but forgot about this pull request. Although there could still be uses of this type of functionality in SwiftyJSON, my specific use case was abusing the library. As such, I built a component in my BetterLibrary that is better designed for my purpose. It is available as a Cocoapod or through SPM. It is fairly mature now, but I would love to have more eyes on it. |
You should consider add @hollyschilling code or add something like
@CosynPa If someone use this pod and new "not beginner friendly" func / var with try in extension is a problem, then this someone will learn how to use try catch 🥇 PS. @hollyschilling BetterLibrary, really good work :) |
any news on making SwiftyJSON throwable? |
@BabyAzerty Read this article and check pod. If you will still need SwiftyJSON with throwable extension then mention me and I will share my version on GitHub |
This extension is useful for non-optional properties in a failable initializer. For example: