Tuesday, September 27, 2022

#1 2022-09-11 07:55:41 pm

From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1458

indexesOfObjectsPassingTest method

Just for learning purposes, I wanted to understand the indexesOfObjectsPassingTest method and wondered if it might be applicable in the following script. If not, is there an example of when it might be used. Thanks.


use framework "Foundation"

set theString to "aa

set searchString to "xx"
set theString to current application's NSString's stringWithString:theString
set theArray to (theString's componentsSeparatedByString:linefeed)
set theIndexes to theArray's indexesOfObjectsPassingTest:[missing code] -- the test is whether an array element contains "xx"

Last edited by peavine (2022-09-11 08:05:23 pm)

2018 Mac mini - macOS Monterey - Script Debugger 8



#2 2022-09-12 12:58:39 am

Nigel Garvey
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5563

Re: indexesOfObjectsPassingTest method

Hi peavine.

The parameter's a "block", according to the documentation. This has no ASObjC equivalent, so I don't think the method can be used in a script.




#3 2022-09-12 07:33:31 am

From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1458

Re: indexesOfObjectsPassingTest method

Thanks Nigel.

2018 Mac mini - macOS Monterey - Script Debugger 8



#4 2022-09-15 01:34:25 am

Registered: 2017-12-14
Posts: 279

Re: indexesOfObjectsPassingTest method

The block is essentially a predicate.

You Could create a predicate.
Get a filteredArray from oroginalArray
Create a NSMutableIndexSet matchIdxs

Then loop thru the filteredArray.
Get aIndex via originalArray indexOfObject:
(As good practice id test the aIndex)
if (aIndex != NSNotfound) then
matchIdxs addIndex:aIndex
end if

The advantages of the block is you can do deeper
Evaluation on each obj and return true/false.
It will automatically add the index if true.
The block also has a STOP pointer variable so at
Any point you can choose to stop the further evaluation
Of any more objects in the original array.

I’ve shifted to mostly programming in Objective-C now.
And as I was learning both AscObjC blocks kept
Coming up.  Since AsObjC couldn’t really
Use them and they seemed a bit confusing, I tended to
Avoid them.   Slowly in Obj-C I Started to
Use the simplest ones like NSArray’s enumerate ItemsUsingBlock:
Lately I’ve really found useful NSPredicate’s predicateWithBlock:
super useful, for more complex filtering and preprocessing the
Evaluated object before my tests. And using another block inside that one
On a NSArray via enumateUsingBlock:
And if I find a match i can use the STOP on that enumerate block



Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)