4 ms·
For example, to filter an array is lovely: filteredArray = [allRecords filteredArrayUsingPredicate: [NSPredicate predicateWithFormat:@"someField == %d",
by mquander 15y ago
For example, to filter an array is lovely:
filteredArray = [allRecords filteredArrayUsingPredicate:
[NSPredicate predicateWithFormat:@"someField == %d", someFieldFilterValue]];
A four-word name for filter and filtering for equality by using a string formatting DSL are not really on my list of lovely things. I'm sure Objective-C does something well but this guy is not pointing it out.
- glenra 15y agoYeah, I found that line hideous - ridiculously unintuitive and hard to read compared to the equivalent in say, ruby. Maybe the whole essay would have been better framed something like: "Objective C: not quite as bad as Java!"
- jasonlotito 15y agoJust curious, for comparison: what would the equivalent be in Ruby?
- akmiller 15y agomy_array.select { |item| item == somefiltervalue } At least that's one way to do it in ruby.
- rbxbx 15y agofilteredArray = [allRecords filteredArrayUsingPredicate: [NSPredicate predicateWithFormat:@"someField == %d", someFieldFilterValue]]; in ruby: all_records.select {|r| r.some_field == some_field_filter_value} in clojure: (filter #(= % some-filter-value) all-records) in haskell: filter (\someField -> x == someFilterValue) allRecords
- icodestuff 15y agoLet's be fair, in Objective-C, this could be actually more like NSIndexSet *indicies = [allRecords indexesOfObjectsPassingTest:^ (id obj, NSUInteger idx, BOOL *stop) { return [obj someField] == someFieldFilterValue; }]; filteredArray = [allRecords objectsAtIndexes:indicies]; rather than what the article had in mind. EDIT: oh, someFieldFilterValue was an integer, wasn't it. Fixed.
- akmiller 15y agoHard to believe anyone would use that as an example. In C# you could do: filteredArray = allRecords.Find(i => i.someField == filterValue) EDIT: Not sure why the downvote, I'm simply stating that many other languages have far better syntax for filtering an array so I'm not sure why the author would think the syntax in Obj-c is all that great unless they have never done the same thing in any other language.
- bonch 15y agoIt's four words because Objective-C methods are self-documenting. From that method name, I know it returns a new, autoreleased array that has been filtered using the provided NSPredicate.