Skip to content

Implement missing stringValue and descriptionWithLocale APIs #277

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

Merged
merged 1 commit into from
Mar 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Foundation/NSDecimalNumber.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class NSDecimalNumber : NSNumber {
NSRequiresConcreteImplementation()
}

public func descriptionWithLocale(locale: AnyObject?) -> String { NSUnimplemented() }
public override func descriptionWithLocale(locale: AnyObject?) -> String { NSUnimplemented() }

// TODO: "declarations from extensions cannot be overridden yet"
// Although it's not clear we actually need to redeclare this here when the extension adds it to the superclass of this class
Expand Down
10 changes: 10 additions & 0 deletions Foundation/NSNumber.swift
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,10 @@ public class NSNumber : NSValue {
return val
}

public var stringValue: String {
return descriptionWithLocale(nil)
}

/// Create an instance initialized to `value`.
public required convenience init(integerLiteral value: Int) {
self.init(integer: value)
Expand All @@ -452,6 +456,12 @@ public class NSNumber : NSValue {
public func compare(otherNumber: NSNumber) -> NSComparisonResult {
return ._fromCF(CFNumberCompare(_cfObject, otherNumber._cfObject, nil))
}

public func descriptionWithLocale(locale: AnyObject?) -> String {
guard let aLocale = locale else { return description }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from a subclassing perspective, iirc this is a bit inverted - descriptionWithLocale should be the funnel point and description should call descriptionWithLocale(nil) but that is a minor 'nit since this is pretty reasonable as it stands.

let formatter = CFNumberFormatterCreate(nil, (aLocale as! NSLocale)._cfObject, kCFNumberFormatterDecimalStyle)
return CFNumberFormatterCreateStringWithNumber(nil, formatter, self._cfObject)._swiftObject
}

override public var _cfTypeID: CFTypeID {
return CFNumberGetTypeID()
Expand Down