Skip to content
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

[SR-72] NSNumber is missing much parity #4475

Open
swift-ci opened this issue Dec 5, 2015 · 6 comments
Open

[SR-72] NSNumber is missing much parity #4475

swift-ci opened this issue Dec 5, 2015 · 6 comments

Comments

@swift-ci
Copy link
Contributor

swift-ci commented Dec 5, 2015

Previous ID SR-72
Radar None
Original Reporter owensd (JIRA User)
Type Bug
Status Reopened
Resolution
Additional Detail from JIRA
Votes 0
Component/s Foundation
Labels Bug
Assignee None
Priority Medium

md5: 67b10dfbfcf6c9bb009224f2a8b42473

is blocked by:

  • SR-408 Foundation requires a factory pattern for creating class clusters

is duplicated by:

  • SR-218 No way to represent boolean types with NSNumber

relates to:

  • SR-457 Null and boolean CF constants are unbridged

Issue Description:

In the ObjC implementation of Foundation, NSNumber knows if it was created with a bool with the __NSCFBoolean type. However, this isn't true for the corelib version.

This functionality breaks our ability to implement serializers like NSJSONSerializer because all bools are treated as generic numbers now.

In addition to this, there is a lot of other functionality that is missing, such as comparison with kCFBooleanTrue values.

If this isn't currently being looked at internally, I can start fixing up these holes.

@swift-ci
Copy link
Contributor Author

swift-ci commented Dec 5, 2015

Comment by David Owens (JIRA)

After digging a bit more, I think this is OK. We can use the CFBoolean type and get by.

@swift-ci
Copy link
Contributor Author

swift-ci commented Dec 5, 2015

Comment by David Owens (JIRA)

Re-opening. I've found another wrinkle.

let b: AnyObject = true

This gets bridged to an NSNumber type. When you have the ObjC runtime, it gets the __NSCFBoolean type.

So there is no way that I can see to get back from a Swift bool value for serialization/deserialization when AnyObject is in the interface.

@lilyball
Copy link
Mannequin

lilyball mannequin commented Dec 6, 2015

FWIW, the way the ObjC Foundation and CoreFoundation work is that creating an NSNumber/CFNumber with a boolean doesn't just give you __NSCFBoolean, it actually explicitly gives you one of the two global constants kCFBooleanTrue or kCFBooleanFalse, and you're supposed to detect that it's a boolean by doing pointer equality against those two constants (since the type __NSCFBoolean is private).

@parkera
Copy link
Member

parkera commented Dec 15, 2015

If (when?) we get proper factory method support from Swift, we should be able to return the private singleton subclasses directly.

@lhoward
Copy link
Contributor

lhoward commented Jan 4, 2016

See also [SR-457] for a workaround but proper factory method support will make fixing this a lot cleaner

@swift-ci
Copy link
Contributor Author

Comment by Jens Alfke (JIRA)

There needs to be a way to get the exact type of the stored number. The boolean case described above is just one example, but there are others:

  • When formatting a floating-point number, you should use 6 decimal places if it's a float, 16 if it's a double. (If you use all 15 for a float, you end up with results like “0.999999999999999” instead of “0.1”.)

  • When getting the integer value of an NSNumber, most of the time you can use longLongValue. But if the internal number is an unsigned 64-bit int you have to use unsignedLongLongValue (and assign it to a UInt64), otherwise it may overflow and appear negative.

In Apple Foundation the `objcType` property lets you do this (except with booleans, which return the wrong type code `c`). But that's obviously a bad name to use in a Swift API…

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@shahmishal shahmishal transferred this issue from apple/swift May 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants