Comments (6)
@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.
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.
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.
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.
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.
@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)
- What is SwiftJSON latest version as per current date(20/06/022 stable version only) which supports iOS 15+ AND what is Alamofire version which supports iOS 15+, both should supports iOS 15 and it's Above version.
- How to test if a property exists on a JSON object? HOT 1
- How to read emojis in JSON
- Warning that in Swift 6 will be an error HOT 13
- Type 'Any' is not optional, value can never be nil; this is an error in Swift 6 HOT 2
- How to update a field of a SwiftyJSON object
- New version release - When? HOT 11
- Json cert
- Recommended way to conform `JSON` to `Hashable` HOT 4
- Empty dictionary object cannot insert data HOT 2
- unwrap crash HOT 1
- Privacy manifest required by Apple HOT 9
- Errors in readme HOT 1
- Privacy Manifest HOT 5
- Is this repository no longer maintained? why? HOT 4
- STACK CANARY problem
- Privacy manifest for application
- PrivacyInfo file HOT 1
- Update to 5.0.2 with cocoapods HOT 10
- Private library that relies on SwiftyJSON, pod lib lint cannot pass HOT 8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from swiftyjson.