I’m doing some reference to understand (KVC) Key-Value Coding Programming with valueForKeyPath method. Here is some example of that using valueForKeyPath and if I understand this correct from NSKeyValueOperator Its a array operator. The examples of anArray variable with objects and values.
I thought I could take keys and values from NSDictionary to become objects in NSArray like
the anArray variable. I did this with AS but it complain about not be KVC compatible array.
Is this possible and also be KVC compatible ??
Or have I miss something…
use framework "Foundation"
use scripting additions
(**
* Key-Value Coding Programming Guide
* Reference:
* https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/KeyValueCoding/index.html
* https://developer.apple.com/documentation/foundation/nskeyvalueoperator?language=objc
*)
(**
* [Instance Method]: valueForKeyPath:
* Returns the value for the derived property identified by a given key path.
**
* A collection operator is one of a small list of keywords preceded by an at sign (@)
* that specifies an operation that the getter should perform to manipulate the data
* in some way before returning it.
*)
set anNewArray to {abc:1000, def:250}
set anArray to {{a:1, e:250, c:3}, {b:250, e:150, c:40}, {b:5, d:7}, {e:100}, {b:5}, {c:10, d:15}, {c:55}, {e:100, f:200}, anNewArray}
set arrayWithValuesAndKeys to (current application's NSMutableArray's arrayWithArray:anArray)
set average to (arrayWithValuesAndKeys's valueForKeyPath:"@avg.b") as integer
log average
set countKeys to (arrayWithValuesAndKeys's valueForKeyPath:"@count") as integer
log countKeys
set maxValue to (arrayWithValuesAndKeys's valueForKeyPath:"@max.c") as integer
log maxValue
set minValue to (arrayWithValuesAndKeys's valueForKeyPath:"@min.d") as integer
log minValue
set sumValue to (arrayWithValuesAndKeys's valueForKeyPath:"@sum.e") as integer
log sumValue
set uniqueKeys to arrayWithValuesAndKeys's valueForKeyPath:"@distinctUnionOfObjects.@allKeys"
log uniqueKeys as list
set uniqueValues to arrayWithValuesAndKeys's valueForKeyPath:"@distinctUnionOfObjects.@allValues"
log uniqueValues as list
set uniqueCollectionValues to arrayWithValuesAndKeys's valueForKeyPath:"@distinctUnionOfObjects.e"
log uniqueCollectionValues as list
set collectionValues to arrayWithValuesAndKeys's valueForKeyPath:"@unionOfObjects.c"
log collectionValues as list
set uniqueKeys to arrayWithValuesAndKeys's valueForKeyPath:"@distinctUnionOfArrays.@allKeys"
log uniqueKeys as list
set uniqueValues to arrayWithValuesAndKeys's valueForKeyPath:"@distinctUnionOfArrays.@allValues"
log uniqueValues as list
set uniqueValues to arrayWithValuesAndKeys's valueForKeyPath:"@distinctUnionOfObjects.abc"
log uniqueValues as list
-- We add a second array so we need to use firstObject()
arrayWithValuesAndKeys's setValue:100 forKey:"abc"
log (arrayWithValuesAndKeys's valueForKeyPath:"@unionOfObjects.abc")'s firstObject() as list
arrayWithValuesAndKeys's setValue:true forKey:"abc"
log arrayWithValuesAndKeys as list
-- Here we could see that bool return NSNumber in this case 1 for true
log (arrayWithValuesAndKeys's valueForKeyPath:"abc")'s firstObject() as integer
log (arrayWithValuesAndKeys's valueForKeyPath:"abc")'s firstObject()'s superclass()'s |description|() as text