holder.valueOfAssets>70

Why is there a “holder” in

also if we really wanted to access holder why don’t we access the class that house the holder instance variable like this

I thought that’s the proper way of accessing ivar in a class?
Please explain tks.

Firstly, I am not quite sure I understand your first question (and don’t have the book in front of me right now), however will pose some sort of answer.

So we have the code:

now, this is saying if the valueOfAssets variable within your holder instance is greater than 70, we do something.

On to your second question. We don’t access the variable by adding BNREmployee at the front, as BNREmployee is the class, and valueOfAssets is an instance variable, and therefore a variable of the holder instance (which is an instance of BNREmployee).

I hope this helps,
Nick

Hi, Fujilla
First of all happy new year.
ok, I understand what this code does

also valueOfAssets is a method, a private method.

there is a holder property in the class BNRAssets like this

@class BNREmployee @property (nonatomic, weak) BNREmployee *holder

back to this code

why dont we do something like this

I mean this is what i’ve learned from this book in the getter and setter section. There are no other parts of the book that explains the
techniques and concept behind this code.
I couldn’t quite get the answer you made on your last post.

That’s not quite accurate.

valueOfAssets is a property of the holder object. However, there is a method behind this property, and that method is also named valueOfAssets even though not declared in the interface file.

[quote]why dont we do something like this

BNRAssets.holder.valueOfAssets>70

I mean this is what i’ve learned from this book in the getter and setter section. There are no other parts of the book that explains the
techniques and concept behind this code.[/quote]
That line of code means: there is an object named BNRAssets (there is one but it is a class object), it (that object) has a property named holder which is also an object with a property named valueOfAssets.

However, that is not the case. The class object BNRAssets does not have a property named holder.

Therefore, we go back to this viable code:

holder.valueOfAssets>70

holder is an object with a property named valueOfAssets.

[quote=“ibex10”][quote]
…ok, I understand what this code does

holder.valueOfAssets>70

also valueOfAssets is a method, a private method.
[/quote]
That’s not quite accurate.

valueOfAssets is a property of the holder object. However, there is a method behind this property, and that method is also named valueOfAssets even though not declared in the interface file.[/quote]

How can a property exist if we don’t declare it like this

or at least that’s what the book shows.
beyond declaring @property i have no idea how a property can exist

Sorry, I don’t have the book. I was just going by the syntax of that line of code.

Having said that, I think I can see your point now.

Try the following code (which doesn’t declare a property):

#import <Foundation/Foundation.h>

@interface Foo : NSObject

- (long)longValue;
- (void)setLongValue:(long)v;

@end

int main (int argc, const char * argv[])
{
    Foo *foo = [[Foo alloc] init];
    
    [foo setLongValue:235711131719];
    foo.longValue = 235711131719;

    long l = [foo longValue];
    l = foo.longValue;

    return 0;
}

@implementation Foo

- (long)longValue
{
    return 0;
}

- (void)setLongValue:(long)v
{
    
}
@end

The code uses the property access syntax even though it does not declare a property named longValue, and it still compiles!

Either this has something to do with Key-Value coding or the compiler is just being nice upon seeing both accessor methods and automatically declaring a property.

So can I use key value coding all when i have similar situation like this?
a pointer to a instance that has a method. like

hold.valueOfAssets

I have been wondering when I am allowed to use the dot notation as well since it was explained the first time in the book as an easier way to use the accessors and after seeing it being used by the authors to call a “casual” method (non accessors), just like in the example mentioned. So some clarification would be great. I have actually been using the dot notation all along with “casual” methods (non accessors) and it has worked fine so far, but I would love to know if this is “bad style”.

Btw., here is some more information on the code mentioned in this thread:

is implemented as

- (unsigned int)valueOfAssets { unsigned int sum = 0; for (BNRAsset *a in _assets) { sum += [a resaleValue]; } return sum; }

I think I may weirdly understand the answer to the original question in this post, which was: why did the authors write “holder.valueOfAssets” as the predicate?

If we step through what the code is doing:

        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"holder.valueOfAssets > 70"];
        NSArray *toBeReclaimed = [allAssets filteredArrayUsingPredicate:predicate];
        NSLog(@"toBeReclaimed: %@", toBeReclaimed);
        toBeReclaimed = nil;

It’s looking through the objects in the NSArray “allAssets,” which contains instances of BNRAsset. Each one of those BNRAsset instances, unless they are unassigned, has a “holder” property that is a pointer to a specific BNREmployee. Each of those BNREmployee “holders,” in turn, has a property called valueOfAssets. So basically, its asking: "Does this asset’s holder have a valueOfAssets that exceeds 70? You don’t need to write it as “BNRAsset.holder.valueOfAssets”—the program already knows that you’re talking about BNRAssets, because that’s what’s in the array that you’re filtering.

I don’t know…that might be wrong, but it makes sense to me. :smiley:

You are correct.

However, the array does not know anything about the objects; it only knows that it can send a message to an object.

This is what the array object does might look like:

...
id object = next object in the array
if ([object valueForKeyPath:@"holder.valueOfAssets"] > 70) {
    ...
}
...

Or:

...
id object = next object in the array
if ([[object holder] valueOfAssets] > 70) {
    ...
}
...

Hello chanyeechoong. I was wondering the same thing and also If
your output was the same as the book when you ran the program.

This is my output and it does not show any assets to be reclaimed.

2014-03-13 11:48:26.516 BMITime[1034:303] Employees: (
"<Employee 0: $0 in assets>",
"<Employee 4: $0 in assets>",
"<Employee 8: $0 in assets>",
"<Employee 7: $384 in assets>",
"<Employee 2: $469 in assets>",
"<Employee 9: $486 in assets>",
"<Employee 1: $503 in assets>",
"<Employee 3: $768 in assets>",
"<Employee 6: $819 in assets>",
"<Employee 5: $836 in assets>"
)
2014-03-13 11:48:26.517 BMITime[1034:303] Giving up ownership of one employee
2014-03-13 11:48:26.518 BMITime[1034:303] allAssets: (
)
2014-03-13 11:48:26.518 BMITime[1034:303] executives: {
CEO = “<Employee 0: $0 in assets>”;
CTO = “<Employee 1: $503 in assets>”;
}
2014-03-13 11:48:26.519 BMITime[1034:303] CEO: <Employee 0: $0 in assets>
[color=#FF0000][size=150]2014-03-13 11:48:26.519 BMITime[1034:303] toBeReclaimed: (
)[/size][/color]
2014-03-13 11:48:26.520 BMITime[1034:303] Giving up ownership of arrays
2014-03-13 11:48:26.520 BMITime[1034:303] deallocating <Employee 0: $0 in assets>
2014-03-13 11:48:26.520 BMITime[1034:303] deallocating <Employee 4: $0 in assets>
2014-03-13 11:48:26.521 BMITime[1034:303] deallocating <Employee 8: $0 in assets>
2014-03-13 11:48:26.521 BMITime[1034:303] deallocating <Employee 7: $384 in assets>
2014-03-13 11:48:26.521 BMITime[1034:303] deallocating <Laptop 2: $384 unassigned>
2014-03-13 11:48:26.522 BMITime[1034:303] deallocating <Employee 2: $469 in assets>
2014-03-13 11:48:26.522 BMITime[1034:303] deallocating <Laptop 7: $469 unassigned>
2014-03-13 11:48:26.523 BMITime[1034:303] deallocating <Employee 9: $486 in assets>
2014-03-13 11:48:26.523 BMITime[1034:303] deallocating <Laptop 8: $486 unassigned>
2014-03-13 11:48:26.524 BMITime[1034:303] deallocating <Employee 3: $768 in assets>
2014-03-13 11:48:26.524 BMITime[1034:303] deallocating <Laptop 0: $350 unassigned>
2014-03-13 11:48:26.524 BMITime[1034:303] deallocating <Laptop 4: $418 unassigned>
2014-03-13 11:48:26.525 BMITime[1034:303] deallocating <Employee 6: $819 in assets>
2014-03-13 11:48:26.525 BMITime[1034:303] deallocating <Laptop 6: $452 unassigned>
2014-03-13 11:48:26.525 BMITime[1034:303] deallocating <Laptop 1: $367 unassigned>
2014-03-13 11:48:26.526 BMITime[1034:303] deallocating <Employee 5: $836 in assets>
2014-03-13 11:48:26.526 BMITime[1034:303] deallocating <Laptop 3: $401 unassigned>
2014-03-13 11:48:26.527 BMITime[1034:303] deallocating <Laptop 5: $435 unassigned>
2014-03-13 11:48:26.527 BMITime[1034:303] deallocating <Employee 1: $503 in assets>
2014-03-13 11:48:26.527 BMITime[1034:303] deallocating <Laptop 9: $503 unassigned>
Program ended with exit code: 0

[quote=“chanyeechoong”]Why is there a “holder” in

On page 185 you’re building a predicate, or a filter, that you will apply against the collection allAssets, which is a NSMutableArray comprised of the asset objects as they are randomly created inside main.m. Remember that each asset description is comprised of 3 parts - self.label, self.resaleValue, self.holder.

The predicate will filter the allAssets collection (again, a NSMutableArray) against the holder property (which is actually a collection in its own right, since it points to the Employee) where valueOfAssets > 70. Meanwhile, valueOfAssets is a public method of Employee which when called returns the sum of the resaleValue of all assets.

Confused yet? :astonished:

[quote=“chanyeechoong”]also if we really wanted to access holder why don’t we access the class that house the holder instance variable like this

BNREmployee is the class that the holder property belongs to. However, in this example, we are accessing it through the Asset classes description (see asset.m and page 166 where asset’s description method was extended to display the holder property).

Mark H