[MacRuby-devel] Getting a corrupt value reading an NSNumber unsignedLongLongValue

Caio Chassot lists at caiochassot.com
Mon Dec 20 10:37:36 PST 2010


I was trying to get the PersistentID from iTunes tracks from both ScriptingBridge and the notifications iTunes posts to NSDistributedNotificationCenter. I noticed I was getting different results from each.

The first byte from the number read from the notification is nearly always garbled (I suspect, only the first two bits). Here's a script that'll show it:

    #!/usr/bin/env macruby
    framework "Cocoa"
    framework 'ScriptingBridge'

    $itunes = SBApplication.applicationWithBundleIdentifier('com.apple.iTunes')

    def trackChange(notification)
      n = notification.userInfo["PersistentID"] # NSNumber
      puts "PersistentID from ScriptingBridge: %s"    % $itunes.currentTrack.persistentID
      puts "PersistentID from notification...: %016X" % n.unsignedLongLongValue
      puts "==="
    end

    NSDistributedNotificationCenter.defaultCenter.addObserver(self, 
      selector:"trackChange:", name:"com.apple.iTunes.playerInfo", object:nil)

    NSRunLoop.currentRunLoop.run


Once running, it'll print the PersistentID from each source every time you skip a track in iTunes. Notice how the first digit tends to differ.

Sample output:

    PersistentID from ScriptingBridge: C06AE8B41249F001
    PersistentID from notification...: 006AE8B41249F001
    ===
    PersistentID from ScriptingBridge: E99C2A8ABA6C7C8F
    PersistentID from notification...: E99C2A8ABA6C7C8F
    ===
    PersistentID from ScriptingBridge: 9993017C397E8601
    PersistentID from notification...: 1993017C397E8601
    ===
    PersistentID from ScriptingBridge: 8C2CACEFCD8BBABA
    PersistentID from notification...: 0C2CACEFCD8BBABA


Here's a similar Nu program that proves the data iTunes posts via the notification is originally correct:

    #!/usr/bin/env nush
    (load "Cocoa")
    (load "ScriptingBridge")

    (set $itunes (SBApplication applicationWithBundleIdentifier: "com.apple.iTunes"))

    (class MyObserver is NSObject
      (imethod (void) trackChange: (id) notification is
        (set v ((notification userInfo) "PersistentID"))
        (system "ruby -e 'puts %q[%016X] % #{(v unsignedLongLongValue)}'")
        (puts (($itunes currentTrack) persistentID))
        (puts "")))

    (set $observer (MyObserver new))

    ((NSDistributedNotificationCenter defaultCenter)
      addObserver:$observer selector:"trackChange:" name:"com.apple.iTunes.playerInfo" object:nil)

    ((NSRunLoop currentRunLoop) run)


And some output from it:

    9566705CB7907985
    9566705CB7907985

    AC4BAAE6BE61E8B1
    AC4BAAE6BE61E8B1

    E4DBB66CFAE503E7
    E4DBB66CFAE503E7

So can we say there's a bug here or am I missing something terribly obvious?

Could someone try to reduce this and take the notification center and iTunes out of the picture and see if it's just a problem with NSNumber?



More information about the MacRuby-devel mailing list