Monday, April 22, 2019

#1 2018-10-30 11:39:59 pm

technomorph
Member
Registered: 2017-12-14
Posts: 103

NSNumber compare functions

Hi so was having lots of trouble with get a AS number from a string.
Finally got it via number formatter's numberFromString: method.
But now I've got an NSNumber.
I've tried to:

Applescript:

as number
as real
as integer
ASify from

and all of the above crash my app.

OK I'll settle with the NSNumber,  but then all of my if/then's were not catching.

So after some more research I found NSNumber's

Applescript:

- isEqualTo:(someNumber)
- compare:(someNumber)

ASObjC did not like the compare method.
so I tried:

Applescript:

- isGreaterThan:(someNumber)
- isLessThan:(someNumber)

and those above work great.
But my if contains:

Applescript:

if ((msgNUMBER ≥ 0) and (msgNUMBER ≤ 58)) then

are there any other comparison methods I can call on an NSNumber
to help me get a isGreaterThanOrEqualTo, isLessThanOrEqualTo?
without having to do

Applescript:

if (myNSNumber's isGreaterThan:(someNumber)) or (myNSNumber's isEqualTo:(someNumber)) then


Filed under: compare, NSNumber, isEqualTo

Offline

 

#2 2018-10-31 12:11:42 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5626

Re: NSNumber compare functions

You can't use "as number", but the "as real" and "as integer" should work (the latter only if the number is within AppleScript's integer range). And "ASify from" should also work, if you have added the framework correctly.

If I were you, I'd have another look at what's going on there.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Online

 

#3 2018-11-01 01:54:20 am

technomorph
Member
Registered: 2017-12-14
Posts: 103

Re: NSNumber compare functions

Shane Stanley wrote:


If I were you, I'd have another look at what's going on there.


I've been working on trying to bring MIDI to ASObjC, trying different frameworks (objC Wrappers)
learning about protocols, subclassing, delegates, callbacks and more.
It's been a great learning experience and I've tried to hammer my way thru it myself.
Eventually I'd love to build a library to share with everyone.

The main problem started with trying to get a value from the array
that's being sent to me via a callback with "me" as the delegate.
The data received is pointer data (i think)
The main values (method instance values) I want are:
channel, data1, data2 and type

No problems getting those values via instance method's using:

Applescript:

msgPtr's channel
msgPtr's data1
msgPtr's data2
msgPtr's type -- problems here, think scripting additions has claimed it 1st
msgPtr's type() -- based on posts you've made and your book, still no
msgPtr's |type| -- nope
msgPtr's |type|() -- nope

-- there is another method i can call on the instance, that returns a string
msgPtr's lengthyDescription
-- returns "Ctrl, ch.0, ctrl.5, val.55"

-- I split the string to a list via lengthyList -> (NSString's string......seperated by as list)
set end of myDescList to lengthyList's item 1
-- repeat with the rest of the list and split each substring with "." delimiter
set subValue to subList's item 2 -- but it's a string (raw format shows as ctxt)
set subValue to subValue as integer -- crash
set subValue to subValue as real -- crash
set theEnd of myDescList to subValue

I think it may have something to do with threading.
As there is constant MIDI being captured by the framework and it is probably
making calls to my callback function, which seems to be ok while I'm in another
function in my script.  But anytime I ask for the bridging it crashes.

Question am I still dealing with pointer Data at this point?
The string I've received would have gone thru a couple of NSString's with string,
separated by processes on it.  When I've checked the class of the string at various
points in the code is always returning _CFTEXT.

I've Figured Out a Solution Around It
to get the type, I just use item 1 from myDescList
and use the values from the framework Class for the others.
so the REAL ISSUE is getting around the instance method named "type"

any insight on the issue?
I've had thoughts about going thru the framework and changing it myself.
adding in my own named instance method that won't conflict.

Applescript:

- (Byte) type; --from the header file

- (Byte) type    { --from the .m file
   return type;
}
- (Byte) channel    { --example of the instance method I can call easy
   return channel;

-- WOULD THIS WORK to solve the |type| conflict?
-- edit the .h and .m file and add my own method

- (Byte) midiType; -- add to the header

- (Byte) midiType    { -- add to the .m file
   return type;
}

Offline

 

#4 2018-11-01 05:07:51 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5626

Re: NSNumber compare functions

It sounds like you have an NSString, in which case you need to do "as text as real".

As for type, if there's a conflict you use pipes: |type|(). the problem is that your header file shows this returns a Byte, and that's not something you can use from ASObjC -- it's a C type, not an Objective-C class.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Online

 

#5 2018-11-02 05:04:42 am

technomorph
Member
Registered: 2017-12-14
Posts: 103

Re: NSNumber compare functions

I've found my own answer to the comparison question
in the foundation framework there is a header file named:

NSScriptWhoseTests.h

inside I found the following comparison methods that will work on NSObject's

Applescript:

@interface NSObject (NSComparisonMethods)
- (BOOL)isEqualTo:(id)object;
// Implemented using isEqual:. Returns NO if receiver is nil.
- (BOOL)isLessThanOrEqualTo:(id)object;
// Implemented using compare. Returns NO if receiver is nil.
- (BOOL)isLessThan:(id)object;
// Implemented using compare. Returns NO if receiver is nil.
- (BOOL)isGreaterThanOrEqualTo:(id)object;
// Implemented using compare. Returns NO if receiver is nil.
- (BOOL)isGreaterThan:(id)object;
// Implemented using compare. Returns NO if receiver is nil.
- (BOOL)isNotEqualTo:(id)object;
// Implemented using compare. Returns NO if receiver is nil.
- (BOOL)doesContain:(id)object;
// Returns nil if receiver is not an NSArray or if array doesn't contain object.
// This operator is not working against the database.
- (BOOL)isLike:(NSString *)object;
// argument should be a string using simple shell wildcards (* and ?).
// (e.g. "Stev*" or "N?XT").
// Returns NO if receiver is not an NSString.
- (BOOL)isCaseInsensitiveLike:(NSString *)
object;
@end

Here are some test I did in Applescript to check them:

Applescript:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use script "BridgePlus"

-- use BridgePlus library to quickly create simple NSObjects
set myNSObject to Cocoaify 5
set myOtherNSObject to Cocoaify 6
set myNSArray to Cocoaify {"hello", 1, 3, "world"}
set myNSArrayItem to Cocoaify "hello"
set myNSString to Cocoaify "world"
set myOtherNSString to Cocoaify "World"

-- NSObject (NSComparisonMethods)

set theResult1 to myNSObject's isEqualTo:(myOtherNSObject) -- returns 0
-- Implemented using isEqual:. Returns NO if receiver is nil.

set theResult2 to myNSObject's isLessThanOrEqualTo:(myOtherNSObject) -- returns 1
-- Implemented using compare. Returns NO if receiver is nil.

set theResult3 to myNSObject's isLessThan:(myOtherNSObject) -- returns 1
-- Implemented using compare. Returns NO if receiver is nil.

set theResult4 to myNSObject's isGreaterThanOrEqualTo:(myOtherNSObject) -- returns 0
-- Implemented using compare. Returns NO if receiver is nil.

set theResult5 to myNSObject's isGreaterThan:(myOtherNSObject) -- returns 0
-- Implemented using compare. Returns NO if receiver is nil.

set theResult6 to myNSObject's isNotEqualTo:(myOtherNSObject) -- returns 1
-- Implemented using compare. Returns NO if receiver is nil.

set theResult7 to myNSArray's doesContain:(myNSArrayItem) -- returns 1
-- Returns nil if receiver is not an NSArray or if array doesn't contain object.
-- This operator is not working against the database.

set theResult8 to myNSString's isLike:(myOtherNSString) -- returns 0
-- argument should be a string using simple shell wildcards (* and ?).
-- (e.g. "Stev*" or "N?XT").
-- Returns NO if receiver is not an NSString.

set theResult9 to myNSString's isCaseInsensitiveLike:(myOtherNSString) -- returns 1

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)