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
mmxxmm
bb
nnxxnn
cc"
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"
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