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-8040] NSNumber cast to UInt64 fails when uint64Value succeeds #3673
Comments
My understanding is that |
Yup, that is 100% on point. |
Ah, I see the weirdness: MPMediaEntityPersistentID is a UInt64, but the NSNumber is being created as a signed 64-bit value. Which wouldn't have made a difference back in Objective-C. |
I'm with you so far, but then how do I convert this thing into a UInt64 correctly using Swift methods rather than calling NSNumber `uint64Value`? It might help if I describe the use case a little more. I have an MPMediaItem, and I say: let id = myMediaItem.value(forProperty:MPMediaItemPropertyAlbumPersistentID) This comes back as an Any?, but it is supposed to be an NSNumber wrapping a CUnsignedLongLong, i.e. a UInt64. I want that number! I used to be able to get it by casting to UInt64 and now I can't. I noticed this because I was successfully force-casting — and then one day I started crashing instead. I am thinking that this might be because SE-1070 changed the behavior of the cast. Of course I can fix this by going back to what I did back in Swift 2, namely cast to NSNumber and call `.uint64Value`. But that seems so unSwifty...! |
Ooooh it looks like you've just answered the question: the issue is buried in the Media Player framework, not in Swift...? |
Okay, I see that I can get the right answer by saying let u : UInt64 = i as? UInt64 ?? UInt64.max - UInt64(-i) + 1 or let u = UInt64.init(bitPattern: Int64(i)) But golly gee. |
FYI your assertion of
Is exactly what uint64Value does under the hood. |
Additional Detail from JIRA
md5: 5276f0131a07c88ff9841cb42fbfa00d
Issue Description:
This arises because with MPMediaItem I'm receiving persistent ID values as NSNumber that I'm having trouble converting to a Swift number. When I log the troublesome values to the console, I get big negative numbers like -341037401921622877 even though this is supposed to be a UInt64. So that's the value I'm using in the example:
My understanding of numerics is a bit shaky, but surely one's instinct is to say that if `uint64Value` can correctly extract the UInt64 value from this NSNumber, then `as? UInt64` should be able to do it as well. And the whole idea of allowing us to cast directly from NSNumber to a Swift numeric type was that it would replace grappling directly with NSNumber methods. Perhaps I've misunderstood https://github.com/apple/swift-evolution/blob/master/proposals/0170-nsnumber_bridge.md but I thought that this was the sort of thing it says ought to work.
The text was updated successfully, but these errors were encountered: