Dot notation vs. brackets


#1

I’m not sure if I’m posting this in the correct place, but I have a question about dot notation. Firstly, I have to say that I’m more a fan of bracket notation, but that might be because i’m not 100% clear on dot notation.

So first of all I understand if I have something like this:

BNRItem *item = [[BNRItem alloc] init];
[item setValueInDollars: 10]; it’s the same as writing item.valueInDollars = 10;

What I don’t understand are things like this self.window.backgroundColor = [UIColor whiteColor]; How could something like this be written using the bracketed notation? It seems writing [self.window setBackgroundColor: [UIColor whiteColor]; works, at least when I build I get no errors. But is there a way to get rid of the . between self & window (i.e turn self.window into [self window])?

I know i must be missing something, but if i see something written with two dots (i.e self.window.backgroundColor or screenRect.origin.x) I’m not clear on what is going on. Ultimately I’m trying to really understand dot notation as it seems to be used a lot, so hopefully My question is clear


#2

Hello, I hope this helps you. Remember that objects have properties. Those properties are accessed using the dot notation. Using the example from the chapter, a BNRItem has certain properties, itemName, serialNumber, valueInDollars, and dateCreated so after creating your item instance you access that items properties with, for instance, item.itemName.
That being said, an object can have another object as a property and that object will have properties. For instance screenRect has an origin property which in turn has an X property. The way to access that X property is by using dot notation to step through the various levels. Give me the screenRect, then give me that items origin point, then give me that origin points X coordinate. Same with self.window.backgroundColor. self of course refers to the instance of the object so give me this instance’s window then give me that window’s background color.

It might help to think of a real world analogy. A House object has Room objects which have Furniture objects. To represent where I’m sitting right now it could be written as myHouse.livingRoom.sofa or even myHouse.livingRoom.sofa.rightSide if we add a side property to our Sofa objects.

I hope this helped clarify dot notation for you.

mb