Wiki source code of WOnder-ERXArrayUtilities
Version 10.1 by Pascal Robert on 2012/02/11 08:27
Show last authors
author | version | line-number | content |
---|---|---|---|
1 | == Overview == | ||
2 | |||
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"]]). | ||
4 | |||
5 | === NSArray operators === | ||
6 | |||
7 | WebObjects provides @sum, @avg, etc array operators. ERXArrayUtilities adds quite a few more: | ||
8 | |||
9 | **SortOperator:** Define an NSArray.Operator for the key //sort//. | ||
10 | |||
11 | This allows for key value paths like: | ||
12 | |||
13 | {{panel}} | ||
14 | myArray.valueForKey("@sort.firstName"); | ||
15 | {{/panel}} | ||
16 | |||
17 | myArray.valueForKey("@sort.lastName,firstName"); | ||
18 | |||
19 | Which in the first case would return myArray sorted ascending by first name and the second case | ||
20 | by lastName and then by firstName. | ||
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}} | ||
29 | myArray.valueForKey("@fetchSpec.fetchUsers"); | ||
30 | {{/panel}} | ||
31 | |||
32 | Which in this case would return myArray filtered and sorted by the | ||
33 | EOFetchSpecification named "fetchUsers" which must be a model-based fetchspec in the | ||
34 | first object's entity. | ||
35 | |||
36 | **FlattenOperator:** Define an NSArray.Operator for the key //flatten//. | ||
37 | |||
38 | This allows for key value paths like: | ||
39 | |||
40 | {{panel}} | ||
41 | myArray.valueForKey("@flatten"); | ||
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}} | ||
51 | myArray.valueForKey("@isEmpty"); | ||
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}} | ||
59 | myArray.valueForKey("@subarrayWithRange.3-20"); | ||
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}} | ||
67 | myArray.valueForKeyPath("@unique.someOtherPath"); | ||
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}} | ||
77 | myArray.valueForKeyPath("@removeNullValues.someOtherPath"); | ||
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}} | ||
87 | myArray.valueForKey("@objectAtIndex.3.firstName"); | ||
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}} | ||
95 | myArray.valueForKey("@avgNonNull.revenue"); | ||
96 | {{/panel}} | ||
97 | |||
98 | which will sum up all values and divide by the number of nun-null entries. | ||
99 | |||
100 | **ReverseOperator:** Define an NSArray.Operator for the key //reverse//. | ||
101 | |||
102 | This allows for key value paths like: | ||
103 | |||
104 | {{panel}} | ||
105 | myArray.valueForKey("@reverse.someMorePath"); | ||
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}} | ||
115 | myArray.valueForKey("@median.someMorePath"); | ||
116 | {{/panel}} | ||
117 | |||
118 | which return the median of the array elements at the given key path. | ||
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. |