Skip to content

Commit 6d107c1

Browse files
authored
Merge pull request #1093 from norio-nomura/use-factory-initializer-on-nsnumber
2 parents d2f8c5e + 829de81 commit 6d107c1

File tree

9 files changed

+207
-114
lines changed

9 files changed

+207
-114
lines changed

CoreFoundation/Base.subproj/ForSwiftFoundationOnly.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,8 @@ CF_PRIVATE void _CFSwiftRelease(void *_Nullable t);
254254

255255
CF_EXPORT void _CFRuntimeBridgeTypeToClass(CFTypeID type, const void *isa);
256256

257+
CF_EXPORT CFNumberType _CFNumberGetType2(CFNumberRef number);
258+
257259
typedef unsigned char __cf_uuid[16];
258260
typedef char __cf_uuid_string[37];
259261
typedef __cf_uuid _cf_uuid_t;

Foundation/JSONEncoder.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
import CoreFoundation
14+
1315
//===----------------------------------------------------------------------===//
1416
// JSON Encoder
1517
//===----------------------------------------------------------------------===//
@@ -1742,7 +1744,7 @@ extension _JSONDecoder {
17421744
}
17431745

17441746
// TODO: Add a flag to coerce non-boolean numbers into Bools?
1745-
guard number._objCType == .Bool else {
1747+
guard number._cfTypeID == CFBooleanGetTypeID() else {
17461748
throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: value)
17471749
}
17481750

Foundation/NSCFBoolean.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,10 @@ internal class __NSCFBoolean : NSNumber {
7878
override var objCType: UnsafePointer<Int8> {
7979
// This must never be fixed to be "B", although that would
8080
// cause correct old-style archiving when this is unarchived.
81-
return UnsafePointer<Int8>(bitPattern: UInt(_NSSimpleObjCType.Char.rawValue.value))!
81+
func _objCType(_ staticString: StaticString) -> UnsafePointer<Int8> {
82+
return UnsafeRawPointer(staticString.utf8Start).assumingMemoryBound(to: Int8.self)
83+
}
84+
return _objCType("c")
8285
}
8386

8487
internal override func _cfNumberType() -> CFNumberType {

Foundation/NSJSONSerialization.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -459,8 +459,8 @@ private struct JSONWriter {
459459
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.propertyListReadCorrupt.rawValue, userInfo: ["NSDebugDescription" : "Number cannot be infinity or NaN"])
460460
}
461461

462-
switch num._objCType {
463-
case .Bool:
462+
switch num._cfTypeID {
463+
case CFBooleanGetTypeID():
464464
serializeBool(num.boolValue)
465465
default:
466466
writer(_serializationString(for: num))

Foundation/NSKeyedUnarchiver.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -683,10 +683,10 @@ open class NSKeyedUnarchiver : NSCoder {
683683
}
684684

685685
open override func decodeBool(forKey key: String) -> Bool {
686-
guard let result : NSNumber = _decodeValue(forKey: key) else {
686+
guard let result : Bool = _decodeValue(forKey: key) else {
687687
return false
688688
}
689-
return result.boolValue
689+
return result
690690
}
691691

692692
open override func decodeInt32(forKey key: String) -> Int32 {

0 commit comments

Comments
 (0)