-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
String to Double? conversion fix #910
base: master
Are you sure you want to change the base?
Conversation
@@ -97,6 +104,12 @@ class NumberTests: XCTestCase { | |||
XCTAssertEqual(json.boolValue, false) | |||
XCTAssertEqual(json.doubleValue, 0.0) | |||
XCTAssertEqual(json.numberValue, 0) | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)
json.string = "swift" | ||
XCTAssertNil(json.number) | ||
XCTAssertEqual(json.numberValue, 0) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)
XCTAssertEqual(json.numberValue.description, "1000000000000000000000000000.1") | ||
|
||
XCTAssertEqual(json.numberValue, 1000000000000000000000000000.1) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)
…Also updated Int code to ensure that "3.14" returns nil (to be more inlines with Int("3.14")), and to return 0 for intValue.
json = "3.14" | ||
XCTAssertNil(json.int) | ||
XCTAssertEqual(json.intValue, 0) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)
XCTAssertEqual(json.intValue, 9876543210) | ||
XCTAssertEqual(json.numberValue, 9876543210) | ||
XCTAssertEqual(json.stringValue, "9876543210") | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)
@@ -140,6 +169,20 @@ class NumberTests: XCTestCase { | |||
XCTAssertEqual(json.int!, 98765421) | |||
XCTAssertEqual(json.intValue, 98765421) | |||
XCTAssertEqual(json.numberValue, NSNumber(value: 98765421)) | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)
XCTAssertEqual(json.floatValue, 9876543210.123456789) | ||
XCTAssertEqual(json.numberValue, 9876543210.123456789) | ||
XCTAssertEqual(json.stringValue, "9876543210.123456789") | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)
@@ -115,6 +133,17 @@ class NumberTests: XCTestCase { | |||
XCTAssertEqual(json.float!, -98766.23) | |||
XCTAssertEqual(json.floatValue, -98766.23) | |||
XCTAssertEqual(json.numberValue, NSNumber(value: -98766.23)) | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)
XCTAssertEqual(json.doubleValue, 9876543210.123456789) | ||
XCTAssertEqual(json.numberValue, 9876543210.123456789) | ||
XCTAssertEqual(json.stringValue, "9876543210.123456789") | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)
Source/SwiftyJSON.swift
Outdated
@@ -1061,6 +1067,13 @@ extension JSON { | |||
|
|||
public var int: Int? { | |||
get { | |||
if let num = self.number as? NSDecimalNumber { | |||
let returnInt = num.intValue | |||
if num.isEqual(returnInt){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Opening Brace Spacing Violation: Opening braces should be preceded by a single space and on the same line as the declaration. (opening_brace)
The PR should summarize what was changed and why. Here are some questions to
help you if you're not sure:
What behavior was changed?
Added code for .double to handle strings. Now returns optional or nil.
What code was refactored / updated to support this change?
// MARK: - Number
extension JSON {
//Optional number
public var number: NSNumber?
What issues are related to this PR? Or why was this change introduced?
When trying to convert a string into a .double, it always returned nil
Checklist - While not every PR needs it, new features should consider this list:
Does this have tests?
Yes
Does this have documentation?
No, just adds optional parity to .doubleValue
Does this break the public API (Requires major version bump)?
Don't believe so
Is this a new feature (Requires minor version bump)?
No, this is a bug fix.