Wiki source code of WOnder-ERXArrayUtilities
Last modified by Pascal Robert on 2012/02/11 08:27
Hide last authors
| author | version | line-number | content |
|---|---|---|---|
| |
7.1 | 1 | == Overview == |
| |
3.1 | 2 | |
| |
10.1 | 3 | ERXArrayUtilities provides convenience methods and tools for manipulating NSArrays. Many of the methods are self explanatory. For a full list please see the api ([[http:~~/~~/wocommunity.org/documents/javadoc/wonder/latester/extensions/foundation/ERXArrayUtilities.html>>url:http://wocommunity.org/documents/javadoc/wonder/latest/er/extensions/foundation/ERXArrayUtilities.html||shape="rect"]]). |
| |
3.1 | 4 | |
| |
7.1 | 5 | === NSArray operators === |
| |
3.1 | 6 | |
| |
9.1 | 7 | WebObjects provides @sum, @avg, etc array operators. ERXArrayUtilities adds quite a few more: |
| |
3.1 | 8 | |
| 9 | **SortOperator:** Define an NSArray.Operator for the key //sort//. | ||
| 10 | |||
| 11 | This allows for key value paths like: | ||
| 12 | |||
| 13 | {{panel}} | ||
| |
7.1 | 14 | myArray.valueForKey("@sort.firstName"); |
| |
3.1 | 15 | {{/panel}} |
| 16 | |||
| |
7.1 | 17 | myArray.valueForKey("@sort.lastName,firstName"); |
| |
3.1 | 18 | |
| 19 | Which in the first case would return myArray sorted ascending by first name and the second case | ||
| |
10.1 | 20 | by lastName and then by firstName. |
| |
3.1 | 21 | |
| 22 | Other sort operators registered are: @sortAsc, @sortDesc, @sortInsensitiveAsc, @sortInsensitiveDesc | ||
| 23 | |||
| 24 | **FetchSpecOperator:** Define an NSArray.Operator for the key //fetchSpec//. | ||
| 25 | |||
| 26 | This allows for key value paths like: | ||
| 27 | |||
| 28 | {{panel}} | ||
| |
7.1 | 29 | myArray.valueForKey("@fetchSpec.fetchUsers"); |
| |
3.1 | 30 | {{/panel}} |
| 31 | |||
| 32 | Which in this case would return myArray filtered and sorted by the | ||
| |
10.1 | 33 | EOFetchSpecification named "fetchUsers" which must be a model-based fetchspec in the |
| 34 | first object's entity. | ||
| |
3.1 | 35 | |
| 36 | **FlattenOperator:** Define an NSArray.Operator for the key //flatten//. | ||
| 37 | |||
| 38 | This allows for key value paths like: | ||
| 39 | |||
| 40 | {{panel}} | ||
| |
7.1 | 41 | myArray.valueForKey("@flatten"); |
| |
3.1 | 42 | {{/panel}} |
| 43 | |||
| 44 | Which in this case would return myArray flattened if myArray is an NSArray of NSArrays (of NSArrays etc). | ||
| 45 | |||
| 46 | **IsEmptyOperator:** Define an NSArray.Operator for the key //isEmpty//. | ||
| 47 | |||
| 48 | This allows for key value paths like: | ||
| 49 | |||
| 50 | {{panel}} | ||
| |
7.1 | 51 | myArray.valueForKey("@isEmpty"); |
| |
3.1 | 52 | {{/panel}} |
| 53 | |||
| 54 | **SubarrayWithRangeOperator:** Define an NSArray.Operator for the key //subarrayWithRange//. | ||
| 55 | |||
| 56 | This allows for key value paths like: | ||
| 57 | |||
| 58 | {{panel}} | ||
| |
7.1 | 59 | myArray.valueForKey("@subarrayWithRange.3-20"); |
| |
3.1 | 60 | {{/panel}} |
| 61 | |||
| 62 | **UniqueOperator:** Define an NSArray.Operator for the key //unique//. | ||
| 63 | |||
| 64 | This allows for key value paths like: | ||
| 65 | |||
| 66 | {{panel}} | ||
| |
7.1 | 67 | myArray.valueForKeyPath("@unique.someOtherPath"); |
| |
3.1 | 68 | {{/panel}} |
| 69 | |||
| 70 | Which in this case would return only those objects which are unique in myArray. | ||
| 71 | |||
| 72 | **RemoveNullValuesOperator:** Define an NSArray.Operator for the key //removeNullValues//. | ||
| 73 | |||
| 74 | This allows for key value paths like: | ||
| 75 | |||
| 76 | {{panel}} | ||
| |
7.1 | 77 | myArray.valueForKeyPath("@removeNullValues.someOtherPath"); |
| |
3.1 | 78 | {{/panel}} |
| 79 | |||
| 80 | Which in this case would return myArray without the occurrences of NSKeyValueCoding.Null. | ||
| 81 | |||
| 82 | **ObjectAtIndexOperator:** Define an NSArray.Operator for the key //objectAtIndex//. | ||
| 83 | |||
| 84 | This allows for key value paths like: | ||
| 85 | |||
| 86 | {{panel}} | ||
| |
7.1 | 87 | myArray.valueForKey("@objectAtIndex.3.firstName"); |
| |
3.1 | 88 | {{/panel}} |
| 89 | |||
| 90 | **AvgNonNullOperator:** Define an NSArray.Operator for the key //avgNonNull//. | ||
| 91 | |||
| 92 | This allows for key value paths like: | ||
| 93 | |||
| 94 | {{panel}} | ||
| |
7.1 | 95 | myArray.valueForKey("@avgNonNull.revenue"); |
| |
3.1 | 96 | {{/panel}} |
| 97 | |||
| |
7.1 | 98 | which will sum up all values and divide by the number of nun-null entries. |
| |
3.1 | 99 | |
| 100 | **ReverseOperator:** Define an NSArray.Operator for the key //reverse//. | ||
| 101 | |||
| 102 | This allows for key value paths like: | ||
| 103 | |||
| 104 | {{panel}} | ||
| |
7.1 | 105 | myArray.valueForKey("@reverse.someMorePath"); |
| |
3.1 | 106 | {{/panel}} |
| 107 | |||
| 108 | which return a reversed result as to you would normally get. | ||
| 109 | |||
| 110 | **MedianOperator:** Define an NSArray.Operator for the key //median//. | ||
| 111 | |||
| 112 | This allows for key value paths like: | ||
| 113 | |||
| 114 | {{panel}} | ||
| |
7.1 | 115 | myArray.valueForKey("@median.someMorePath"); |
| |
3.1 | 116 | {{/panel}} |
| 117 | |||
| 118 | which return the median of the array elements at the given key path. | ||
| |
10.1 | 119 | The median is the value for which half of the elements are above and half the elements are below. |
| 120 | As such, an array sort is needed and this might be very costly depending of the size of the array. |