• 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
Re: How Do I Handle NSNumber Returned in ASObjC?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How Do I Handle NSNumber Returned in ASObjC?


  • Subject: Re: How Do I Handle NSNumber Returned in ASObjC?
  • From: Shane Stanley <email@hidden>
  • Date: Mon, 13 Jun 2016 10:04:47 +1000

On 13 Jun 2016, at 9:37 AM, Jim Underwood <email@hidden> wrote:
>
> 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?

First, there's no guarantee it will be an __NSCFNumber -- that's just one of several possible subclasses of NSNumber it could be. Rather than get the class, you check for any subclass like this:

	if (theResult's isKindOfClass:(current application's NSNumber)) as boolean then

That checks if it's any subclass of NSNumber.

But the real/integer/boolean problem (and more) can be solved without any of that. Rather, you can let the scripting bridge do the work for you. So after you have checked for missing value:

  set theResult to theDict's objectForKey:pKeyStr
  set someArray to current application's NSArray's arrayWithObject:theResult
  return item 1 of (someArray as list)

What this does is makes a single-item array containing theResult. When that's converted to an AppleScript list, the item within it will also be converted to an AppleScript item if it can. So this covers all the bridgeable AS classes -- text, numbers, lists, records, whatever. You then extract the item from the list.

--
Shane Stanley <email@hidden>
<www.macosxautomation.com/applescript/apps/>


 _______________________________________________
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>
    • Re: How Do I Handle NSNumber Returned in ASObjC?
      • From: Jim Underwood <email@hidden>
References: 
 >How Do I Handle NSNumber Returned in ASObjC? (From: Jim Underwood <email@hidden>)

  • Prev by Date: How Do I Handle NSNumber Returned in ASObjC?
  • Next by Date: Re: How Do I Handle NSNumber Returned in ASObjC?
  • Previous by thread: How Do I Handle NSNumber Returned in ASObjC?
  • Next by thread: Re: How Do I Handle NSNumber Returned in ASObjC?
  • Index(es):
    • Date
    • Thread