Giter VIP home page Giter VIP logo

Comments (6)

LukeTangPL avatar LukeTangPL commented on May 22, 2024

@dhoerl thank you very much. The NSNull Category which you post on stackoverflow in Objective-C is a masterpiece solution.
There are few way in SwiftyJSON to get NSNull object are var null: NSNull? and var object: AnyObject(better not use), other methods and properties that return the swift type in SwfityJSON are safe and reliable. such as:

let data: NSData = dataFromNetworking()
let json = JSON(data: data)
if let mayBeNull = json.null {
    //It's null
} else {
    //It's not null
}
if let string = json.string {
     //It's String or NSString
} {
     //It's not String or NSString
}

// If json is .String return the object as String, 
// if json is.Number or .Bool convert the object to String
// Otherwise return empty string
let iDontCare = json["iDontCare"].stringValue 

It's seem like NSNull Category.
If your have any idea, please let me know or submit a pull request.
Thanks.

from swiftyjson.

antoinerabanes avatar antoinerabanes commented on May 22, 2024

Ideally if the value is null, the key should be removed.
in turn when you query the collection for this key it will return nil

from swiftyjson.

dhoerl avatar dhoerl commented on May 22, 2024

I completely disagree on deleting the key. A 'null' JSON value is a valid return, and has meaning. I've experience with actual e-commerce JSON replies containing:

inventory : null // means it hasn't been entered yet
inventory: 50 // in stock at that level
inventory: -3 // yeah, got this! - the stupid e-commerce site sold 3 items before it realized it didn't have them in stock, so someone is on the phone now trying to calm a customer down (this really happened and crashed my app - I had coded inventory as a unsigned integer!)

from swiftyjson.

antoinerabanes avatar antoinerabanes commented on May 22, 2024

Thats interesting insight and a good example of the complexity of integrating an API that returns null/NSNull.
However I would expect an integer back from the API and zero is a valid value to represent the stock when null is not because it is a different type and does not represent a number..

Sent from my iPhone

On 30 Jan 2015, at 17:00, David Hoerl [email protected] wrote:

I completely disagree on deleting the key. A 'null' JSON value is a valid return, and has meaning. I've experience with actual e-commerce JSON returns containing such:

inventory : null // means it hasn't been entered yet
inventory: 50 // in stock at that level
inventory: -3 // yeah, got this - the stupid e-commerce site sold 3 items before it realized it didn't have them in stock, so someone is on the phone now trying to calm a client down (yeah, this happened and crashed my app!)


Reply to this email directly or view it on GitHub.

from swiftyjson.

dhoerl avatar dhoerl commented on May 22, 2024

All I can say is my comment reflects Apple's current thinking. Swift Optionals either have a value or don't. The value can be 0 or not (if its say an Int). Each of these three states (as stated in my earlier example) means something different. A framework like this one cannot afford (IMHO) to gloss over this and assume all users want "no value" to be mapped into "0" (or similar).

That said, after fussing with nil for weeks, I threw in the towel and created the NSNull category above, which gives you exactly what you want - two cases. Even so, I prefer this framework to do the proper thing, and let the user decide to map 3 states to 2 if desired.

from swiftyjson.

Nelyus avatar Nelyus commented on May 22, 2024

@dhoerl You could something like this, without any change to JSON:

if let number = json.number?.integerValue ?? json.null?.integerValue {
  // do stuff
}

It'd be more explicit, which can be good.

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.