Skip to content

Commit d474259

Browse files
committed
Implement missing stringValue and descriptionWithLocale APIs
1 parent a1d15a1 commit d474259

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

Foundation/NSDecimalNumber.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public class NSDecimalNumber : NSNumber {
5555
NSRequiresConcreteImplementation()
5656
}
5757

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

6060
// TODO: "declarations from extensions cannot be overridden yet"
6161
// Although it's not clear we actually need to redeclare this here when the extension adds it to the superclass of this class

Foundation/NSNumber.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,10 @@ public class NSNumber : NSValue {
434434
return val
435435
}
436436

437+
public var stringValue: String {
438+
return descriptionWithLocale(nil)
439+
}
440+
437441
/// Create an instance initialized to `value`.
438442
public required convenience init(integerLiteral value: Int) {
439443
self.init(integer: value)
@@ -452,6 +456,12 @@ public class NSNumber : NSValue {
452456
public func compare(otherNumber: NSNumber) -> NSComparisonResult {
453457
return ._fromCF(CFNumberCompare(_cfObject, otherNumber._cfObject, nil))
454458
}
459+
460+
public func descriptionWithLocale(locale: AnyObject?) -> String {
461+
guard let aLocale = locale else { return description }
462+
let formatter = CFNumberFormatterCreate(nil, (aLocale as! NSLocale)._cfObject, kCFNumberFormatterDecimalStyle)
463+
return CFNumberFormatterCreateStringWithNumber(nil, formatter, self._cfObject)._swiftObject
464+
}
455465

456466
override public var _cfTypeID: CFTypeID {
457467
return CFNumberGetTypeID()

0 commit comments

Comments
 (0)