Wiki source code of Programming__WebObjects-WOnder-ERXArrayUtilities
Version 5.1 by smmccraw on 2007/07/08 09:46
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:~/~/webobjects.mdimension.com/wonder/api/er/extensions/ERXArrayUtilities.html). | ||
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 | |||
15 | myArray.valueForKey("@sort.firstName"); | ||
16 | |||
17 | {{/panel}} | ||
18 | |||
19 | myArray.valueForKey("@sort.lastName,firstName"); | ||
20 | |||
21 | Which in the first case would return myArray sorted ascending by first name and the second case | ||
22 | by lastName and then by firstName. | ||
23 | |||
24 | Other sort operators registered are: @sortAsc, @sortDesc, @sortInsensitiveAsc, @sortInsensitiveDesc | ||
25 | |||
26 | **FetchSpecOperator:** Define an NSArray.Operator for the key //fetchSpec//. | ||
27 | |||
28 | This allows for key value paths like: | ||
29 | |||
30 | {{panel}} | ||
31 | |||
32 | myArray.valueForKey("@fetchSpec.fetchUsers"); | ||
33 | |||
34 | {{/panel}} | ||
35 | |||
36 | Which in this case would return myArray filtered and sorted by the | ||
37 | EOFetchSpecification named "fetchUsers" which must be a model-based fetchspec in the | ||
38 | first object's entity. | ||
39 | |||
40 | **FlattenOperator:** Define an NSArray.Operator for the key //flatten//. | ||
41 | |||
42 | This allows for key value paths like: | ||
43 | |||
44 | {{panel}} | ||
45 | |||
46 | myArray.valueForKey("@flatten"); | ||
47 | |||
48 | {{/panel}} | ||
49 | |||
50 | Which in this case would return myArray flattened if myArray is an NSArray of NSArrays (of NSArrays etc). | ||
51 | |||
52 | **IsEmptyOperator:** Define an NSArray.Operator for the key //isEmpty//. | ||
53 | |||
54 | This allows for key value paths like: | ||
55 | |||
56 | {{panel}} | ||
57 | |||
58 | myArray.valueForKey("@isEmpty"); | ||
59 | |||
60 | {{/panel}} | ||
61 | |||
62 | **SubarrayWithRangeOperator:** Define an NSArray.Operator for the key //subarrayWithRange//. | ||
63 | |||
64 | This allows for key value paths like: | ||
65 | |||
66 | {{panel}} | ||
67 | |||
68 | myArray.valueForKey("@subarrayWithRange.3-20"); | ||
69 | |||
70 | {{/panel}} | ||
71 | |||
72 | **UniqueOperator:** Define an NSArray.Operator for the key //unique//. | ||
73 | |||
74 | This allows for key value paths like: | ||
75 | |||
76 | {{panel}} | ||
77 | |||
78 | myArray.valueForKeyPath("@unique.someOtherPath"); | ||
79 | |||
80 | {{/panel}} | ||
81 | |||
82 | Which in this case would return only those objects which are unique in myArray. | ||
83 | |||
84 | **RemoveNullValuesOperator:** Define an NSArray.Operator for the key //removeNullValues//. | ||
85 | |||
86 | This allows for key value paths like: | ||
87 | |||
88 | {{panel}} | ||
89 | |||
90 | myArray.valueForKeyPath("@removeNullValues.someOtherPath"); | ||
91 | |||
92 | {{/panel}} | ||
93 | |||
94 | Which in this case would return myArray without the occurrences of NSKeyValueCoding.Null. | ||
95 | |||
96 | **ObjectAtIndexOperator:** Define an NSArray.Operator for the key //objectAtIndex//. | ||
97 | |||
98 | This allows for key value paths like: | ||
99 | |||
100 | {{panel}} | ||
101 | |||
102 | myArray.valueForKey("@objectAtIndex.3.firstName"); | ||
103 | |||
104 | {{/panel}} | ||
105 | |||
106 | **AvgNonNullOperator:** Define an NSArray.Operator for the key //avgNonNull//. | ||
107 | |||
108 | This allows for key value paths like: | ||
109 | |||
110 | {{panel}} | ||
111 | |||
112 | myArray.valueForKey("@avgNonNull.revenue"); | ||
113 | |||
114 | {{/panel}} | ||
115 | |||
116 | which will sum up all values and divide by the number of nun-null entries. | ||
117 | |||
118 | **ReverseOperator:** Define an NSArray.Operator for the key //reverse//. | ||
119 | |||
120 | This allows for key value paths like: | ||
121 | |||
122 | {{panel}} | ||
123 | |||
124 | myArray.valueForKey("@reverse.someMorePath"); | ||
125 | |||
126 | {{/panel}} | ||
127 | |||
128 | which return a reversed result as to you would normally get. | ||
129 | |||
130 | **MedianOperator:** Define an NSArray.Operator for the key //median//. | ||
131 | |||
132 | This allows for key value paths like: | ||
133 | |||
134 | {{panel}} | ||
135 | |||
136 | myArray.valueForKey("@median.someMorePath"); | ||
137 | |||
138 | {{/panel}} | ||
139 | |||
140 | which return the median of the array elements at the given key path. | ||
141 | The median is the value for which half of the elements are above and half the elements are below. | ||
142 | As such, an array sort is needed and this might be very costly depending of the size of the array. | ||
143 | |||
144 | Category:WebObjects |