Dot Notation?


#1

Given the previous caution against dot notation earlier in the book and on the BNR website, why is it used here:?

 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"holder.valueOfAssets > 70"];

Thanks in advance.


#2

Dot notation is, so far as I recall, mandatory when using key-value coding.

So if you’re specifying a path to a value using a string like that, it’s your only option.


#3

We oppose using dots to execute methods – that’s “dot notation”.

We are not against the period character altogether. In this case, the period is expressing the idea of a key-path in a predicate string. This is neither offensive to us, nor avoidable.


#4

@macintux: Wow thanks! I didn’t realize it is mandatory for key-value string predicates. Going to have to give the key-value documents a good read.

@AaronHillegass: At least for me, the confusion (dot notation is bad) came from the getters and setters. Having only learned one other OOP language (python) where you don’t set-up methods for assigning or retrieving values to/from a property, it was easy for me to accept avoiding the dot all together. Thanks for the clarification.

@jemicha: This helped me clear the fog --> http://weblog.bignerdranch.com/?p=83


#5

By the way, I’m not quite understand why use “holder” here. So I’ve tried to change it to “asset”, but things goes wrong in the result.

2012-07-04 14:08:39.811 BMITime[848:403] Employees: ( "<Employee 0: $0 in assets>", "<Employee 4: $0 in assets>", "<Employee 8: $0 in assets>", "<Employee 7: $34 in assets>", "<Employee 3: $68 in assets>", "<Employee 2: $119 in assets>", "<Employee 6: $119 in assets>", "<Employee 5: $136 in assets>", "<Employee 9: $136 in assets>", "<Employee 1: $153 in assets>" ) 2012-07-04 14:08:39.813 BMITime[848:403] Giving up ownership of one employee 2012-07-04 14:08:39.813 BMITime[848:403] deallocating <Employee 2: $119 in assets> 2012-07-04 14:08:39.814 BMITime[848:403] allAssets: ( "<Laptop 0: $0, assigned to <Employee 3: $68 in assets>>", "<Laptop 1: $17, assigned to <Employee 6: $119 in assets>>", "<Laptop 2: $34, assigned to <Employee 7: $34 in assets>>", "<Laptop 3: $51, assigned to <Employee 5: $136 in assets>>", "<Laptop 4: $68, assigned to <Employee 3: $68 in assets>>", "<Laptop 5: $85, assigned to <Employee 5: $136 in assets>>", "<Laptop 6: $102, assigned to <Employee 6: $119 in assets>>", "<Laptop 7: $119 unassigned>", "<Laptop 8: $136, assigned to <Employee 9: $136 in assets>>", "<Laptop 9: $153, assigned to <Employee 1: $153 in assets>>" ) 2012-07-04 14:08:39.815 BMITime[848:403] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<Asset 0x107214d90> valueForUndefinedKey:]: this class is not key value coding-compliant for the key asset.' *** First throw call stack: ( 0 CoreFoundation 0x00007fff8d9dff56 __exceptionPreprocess + 198 1 libobjc.A.dylib 0x00007fff8e6c7d5e objc_exception_throw + 43 2 CoreFoundation 0x00007fff8da6a1b9 -[NSException raise] + 9 3 Foundation 0x00007fff897ac703 -[NSObject(NSKeyValueCoding) valueForUndefinedKey:] + 240 4 Foundation 0x00007fff896e338e _NSGetUsingKeyValueGetter + 108 5 Foundation 0x00007fff896e3315 -[NSObject(NSKeyValueCoding) valueForKey:] + 392 6 Foundation 0x00007fff89702d50 -[NSObject(NSKeyValueCoding) valueForKeyPath:] + 266 7 Foundation 0x00007fff8973a352 -[NSFunctionExpression expressionValueWithObject:context:] + 821 8 Foundation 0x00007fff89739f94 -[NSComparisonPredicate evaluateWithObject:substitutionVariables:] + 206 9 Foundation 0x00007fff89739a9a -[NSPredicate evaluateWithObject:] + 18 10 Foundation 0x00007fff89739a1f _filterObjectsUsingPredicate + 332 11 Foundation 0x00007fff89739870 -[NSArray(NSPredicateSupport) filteredArrayUsingPredicate:] + 320 12 BMITime 0x0000000107154bdf main + 1215 13 BMITime 0x0000000107154714 start + 52 14 ??? 0x0000000000000001 0x0 + 1 ) terminate called throwing an exception(lldb)

Would someone please explain it to me?


#6

Good question. The filter is being applied against assets, as you point out, but we’re specifically trying to identify those held by an employee who has a certain value of assets.

Each asset has an instance variable named holder, which refers to the employee who currently holds it, so the format string refers to that instance variable, and references the method valueOfAssets which is defined in Employee.m.