Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
May 26, 2018, 08:48:42 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
   Home   Help Search Login Register Chat  
Pages: [1]   Go Down
Author Topic: Chapter 8, 6th edition - Stuck with fundamentals  (Read 656 times)
Posts: 2

« on: February 24, 2017, 06:44:33 AM »

Hello, I'm trying to learn Objective-C and am stuck in Chapter 8. Would appreciate if someone can get me out of the rut.

My worries concern Class Rectangle that is introduced in this chapter (Cool.

1. First I'm troubled by the fact that nowhere is it said that Properties height and width of Rectangle must not be negative. If they can be negative, the definition of Property origin (coordinates of the lower left corner of the rectangle) makes that origin has to be recomputed (and the negative values of height / with converted to positives) each time height and width are (re)set. There are also a number of interesting complications if the user sets first the height and the width of the rectangle before setting its origin.

To avoid these complications I would like to ensure that height and width are >= 0. But as author Kochan has imposed earlier that these Properties are @synthesized I assume that the author does not wish that I tinker with their accessor methods ...

Any views on this ?

2. More fundamental : setOrigin method implementation. The book usefully highlights the problem that can occur if this method just copies into origin (which is a pointer) the parameter pt (also a pointer) that is passed to it : both pointers point to the same values and if the values that pt points to are changed so will the values that origin points to.

The book says that the solution is for the instance to own its own origin XYPoint. This is achieved through a preliminary statement at the beginning of the method

origin = [[XYPoint] alloc] init];

Very well. Now we have created our own XYPoint which origin points to.

My problem is that this looks strange. The statement is put inside the setOrigin method. Presumably, the XYPoint that it creates is created in memory space that belongs to the method for the duration of its execution. By the time the method returns, this space is reverted to the 'space unused' pool and nothing guarantees us that the space where the x and y property values of origin are stored will not be overwritten by someone else.

I would have thought that the right solution would be to make the allocation / initialization of the variable along with its declaration in this way :

@interface : Rectangle
   XYPoint origin * = [[XYPoint alloc] init]];

but this is not allowed.

Can someone please shed some light into this ?

Many thanks in advance.
Pages: [1]   Go Up
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents ゥ 2009 All rights reserved.