Coder Social home page Coder Social logo

Comments (6)

lingoer avatar lingoer commented on May 19, 2024

Well, I don't think so.
In my opinion, retrieving value with subscript is still in the context of JSON,when and only when the time you try to cast the real value out from JSON should then be an Optional.
Additionally it also makes it possible to trace the breaking point of the subscript chain.

from swiftyjson.

pvwoods avatar pvwoods commented on May 19, 2024

Whenever a subscript object can not be guaranteed to exist, it would seem logical to cast it as an optional in order to avoid people assuming that it should exist. This is an issue that JSON is specifically susceptible to since people will often treat elements of a JSON object as being required, when in reality, it can only be assumed that it will usually exist, though it is not guaranteed.

from swiftyjson.

mogocat avatar mogocat commented on May 19, 2024

I think that if the value exists, the code returns the value, if not, the code returns something about which subscript is nil. This may be very helpful when my json data has many levels. For example:{foo:{bar:{x:{y:"value"}}}}. When I type some code like: json['foo']['bar']['z']['y'].string, I want to know that the subscript "z" isn't exist, not only return nil.

from swiftyjson.

LukeTangPL avatar LukeTangPL commented on May 19, 2024

@liuchunyao90 try this

if let string = json['foo']['bar']['z']['y'].string {
    // do something you like
} else {
    // print the error
   println(json['foo']['bar']['z']['y'])
}

or

if let error = json["a"]["b"]["c"]["d"][0].error {
     //done. the json is the same like you expect
} else {
    // print the error
    println(error)
}

from swiftyjson.

k06a avatar k06a commented on May 19, 2024

Maybe it will be nice to use library like this?

switch (json['foo']['bar']['z']['y'].stringOrError()) {
case .Error(let error):
    break;
case .Some(let some):
    break;
}

this method should be like this:

func stringOrError() -> OptionalError<String>

where OptionalError should be like this:

enum OptionalError<T> {
    case Error(NSError)
    case Some(T)
}

from swiftyjson.

k06a avatar k06a commented on May 19, 2024

@FlappyHeart you really need to know this in run time? Structure format should be selected in compile time mostly.

from swiftyjson.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.