• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
How Do I Handle NSNumber Returned in ASObjC?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

How Do I Handle NSNumber Returned in ASObjC?


  • Subject: How Do I Handle NSNumber Returned in ASObjC?
  • From: Jim Underwood <email@hidden>
  • Date: Sun, 12 Jun 2016 23:37:38 +0000
  • Thread-topic: How Do I Handle NSNumber Returned in ASObjC?

Well, I am in well over my head here, but thanks to Shane's great book Everyday AppleScriptObjC, Third Edition, I have been able to cobble together a handler to return values of an AppleScript Record.

My question is, when the record value for a key is a number (could be integer or real), what is the best way to handle it?

ASObjC returns a class of "NSNumber" (actually, Script Debugger reports the class as "(Class) __NSCFNumber").  I need to convert that into an AppleScript number, but integer or real?

I see no alternative other than real.  But I thought I'd ask the experts here.

From Shane's book:


If you have been returned an NSNumber, unless you are going to pass it to another method, you will need
to coerce it to an AppleScript number. You do this using as integer or as real, depending on which is
appropriate. Importantly (and unfortunately), you cannot use as number — that will generate an error.


My Script:


set myRec to {SomeNumber:3.14, |name|:"nemo", address:"Tokyo", language:"Latin", vehicle:"tandem"}

set someValue to my getRecValue("SomeNumber", myRec)


--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

on getRecValue(pKeyStr, pRecord)

  ------------------------------------

  

  local theDict, theResult, classResult, classResultStr

  

  set theDict to current application's NSDictionary's dictionaryWithDictionary:pRecord

  set theResult to theDict's valueForKey:pKeyStr

  

  set classResult to class of theResult

  set classResultStr to classResult's className() as text

  

  if theResult = missing value then

    set theResult to missing value

  else

    if classResultStr = "__NSCFNumber" then

      set theResult to theResult as real

    else

      set theResult to theResult as text

    end if

  end if

  

  return theResult

  

end getRecValue





Best Regards,

Jim Underwood
aka JMichaelTX

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

  • Follow-Ups:
    • Re: How Do I Handle NSNumber Returned in ASObjC?
      • From: Shane Stanley <email@hidden>
  • Prev by Date: Script Debugger (and UI Browser)
  • Next by Date: Re: How Do I Handle NSNumber Returned in ASObjC?
  • Previous by thread: Script Debugger (and UI Browser)
  • Next by thread: Re: How Do I Handle NSNumber Returned in ASObjC?
  • Index(es):
    • Date
    • Thread