Object C

Page 22

CHAPTER 1

Objects, Classes, and Messaging

aVariable = anObject.aProperty;

The type of the aProperty property and the type of the aVariable variable must be compatible, otherwise the compiler issues a warning. ●

The following statement invokes the setName: setter method on the anObject object, passing @"New Name" as the method’s parameter: anObject.name = @"New Name";

The compiler issues a warning if the setName: method does not exist, if the property name does not exist, or if the setName: method returns anything but void. ●

The following statement invokes the bounds method on the aView object. It then assigns to the xOrigin variable the value of the origin.x structure element of the NSRect object returned by the bounds method. xOrigin = aView.bounds.origin.x;

The following statements result in the assignment of the value of 11 to two properties: the integerProperty property of the anObject object and the floatProperty property of the anotherObject object. NSInteger i = 10; anObject.integerProperty = anotherObject.floatProperty = ++i;

That is, the rightmost assignment is preevaluated and the result is passed to the setIntegerProperty: and setFloatProperty: setter methods. The data type of the preevaluated result is coerced as required at each point of assignment.

Incorrect Use of Dot Syntax The code patterns that follow are strongly discouraged because they do not conform to the intended use of dot syntax, namely for invoking an accessor method. ●

The following statement generates a compiler warning (warning: value returned from property not used.). anObject.retain;

The following code generates a compiler warning that setFooIfYouCan: does not appear to be a setter method because it does not return (void). /* Method declaration. */ - (BOOL) setFooIfYouCan: (MyClass *)newFoo; /* Code fragment. */ anObject.fooIfYouCan = myInstance;

The following statement invokes the lockFocusIfCanDraw method and assigns the return value to flag. It does not generate a compiler warning unless there is a mismatch between the type for flag and the method’s return type. Nonetheless, this pattern is strongly discouraged. flag = aView.lockFocusIfCanDraw;

22

Object Messaging 2010-12-08 | © 2010 Apple Inc. All Rights Reserved.


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.