Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Added methods that throw errors if the type is not what is expected.#487

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
hollyschilling wants to merge1 commit intoSwiftyJSON:master
base:master
Choose a base branch
Loading
fromhollyschilling:master

Conversation

@hollyschilling
Copy link

@hollyschillinghollyschilling commentedMar 30, 2016
edited
Loading

This extension is useful for non-optional properties in a failable initializer. For example:

class Foo {    var one : String = ""    var two : Int = 0    var optionalFloat : Float?     init?(json : JSON) {        do {            one = try json["one"].requiredString()            two = try json["two"].requiredInt()        } catch {            return nil        }        optionalFloat = json["optionalFloat"].float    }}

CosynPa, mudox, kszuba, and rehannali reacted with thumbs up emojicharlag and mudox reacted with heart emoji
@jkubicek
Copy link

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 (func string() throws -> String) implies that the property would be required.

@charlag
Copy link

I would love to use this!

@CosynPa
Copy link

@jkubicek I think this is confusing since we already have string property.

@CosynPa
Copy link

I think it's better to use therawString,rawArray series API instead of thestringValue,arrayValue API to implement these methods. And I wishrequiredDictionaryObject which returns[String: AnyObject] andrequiredArrayObject which returns[AnyObject] are added too.

@CosynPa
Copy link

AndrequiredBool is a must, since boolean type is different type from number type.

@zhigang1992zhigang1992 added this to the3.1 milestoneSep 21, 2016
@zhigang1992
Copy link
Contributor

Looks interesting, can you kindly add a bit of test and update the syntax to swift3.
Would love to ship this.

Also, sorry for not getting to this sooner.

@zhigang1992zhigang1992 modified the milestones:3.1,3.2Sep 26, 2016
@zhigang1992zhigang1992 removed this from the3.2 milestoneOct 7, 2016
@ldiqual
Copy link
Contributor

ldiqual commentedNov 22, 2016
edited
Loading

@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

  • json.string -> String?
  • json.stringValue -> String with a fallback on"" if not found, potentially error prone
  • json.requiredString throws -> String which throws if the value is incorrect.

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:

  • json.string() throws -> String

It covers all the cases above:

  • try? json.string()
  • try? json.string() ?? ""
  • try json.string()

It also has the value to provide an explicit error if the unpack fail, as opposed to returningnil or a "fake" value. Imho it'll lead to more robustness in codebases.

However it does introduce a code-breaking change, probably worth a SwiftyJSON v4 or something. What do you think?

MontakOleg, edjiang, MikotoZero, jgriffin-isp, hollyschilling, mudox, kszuba, and rehannali reacted with thumbs up emoji

@CosynPa
Copy link

@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.

@hollyschilling
Copy link
Author

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 myBetterLibrary 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.

@kszuba
Copy link

kszuba commentedDec 4, 2017
edited
Loading

You should consider add@hollyschilling code or add something liketryString, because good mapping in reactive base project (like RxSwift) practically base ontry!

I don't think the try syntax is very beginner friendly or easy to use

@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 :)

@Kalzem
Copy link

any news on making SwiftyJSON throwable?

@kszuba
Copy link

@BabyAzerty
I created new pod JSONModel with throwable extension. I can share it for you, but IMO the best option for throwable mapping is use:
https://hackernoon.com/everything-about-codable-in-swift-4-97d0e18a2999
https://github.com/quicktype/quicktype-xcode?utm_campaign=iOS%2BDev%2BWeekly&utm_medium=email&utm_source=iOS_Dev_Weekly_Issue_333

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

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

No reviews

Assignees

No one assigned

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

8 participants

@hollyschilling@jkubicek@charlag@CosynPa@zhigang1992@ldiqual@kszuba@Kalzem

[8]ページ先頭

©2009-2025 Movatter.jp