• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Error in learning Cocoa book?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Error in learning Cocoa book?


  • Subject: Error in learning Cocoa book?
  • From: Ricky Sharp <email@hidden>
  • Date: Sat, 13 Oct 2001 10:45:54 -0500

All,

In Chapter 9, page 162, readers are instructed to add an instance variable to the Expenses class:

NSMutableArray* expenses;

and then an appropriate accessor. The book has the method declaration as:

- (NSMutableArray*) expenses;

but the implementation as:

- (NSArray*) expenses
{
return expenses;
}

Now, this does compile, link and run, but are there any side effects of this? If anything, I would have thought that the declaration would also have a return type of NSArray*. Or, both declaration and implementation would have a return type of NSMutableArray*.

Let's assume that callers may or may not need mutable forms of the array and the return type was made NSArray* in both cases.

if I then have...

NSArray* arrayOne = [anInstanceOfExpenses expenses];
NSMutableArray* arrayTwo = [anInstanceOfExpenses expenses];

What happens here? Similar question to if the Expenses class has the return type of NSMutableArray...what happens?


I guess I'm seeing something similar to what you do in C++. e.g.

class Foo
{
private:
char* something;

public:
const char* operator * ( void ) { return something; }
char* operator * ( void ) { return something; }
}

Which allows callers to get acquire a const (read-only) or non-const (read-write) pointer when using operator *.


TIA,

------------------------------------------------------------
Ricky A. Sharp Instant Interactive(tm)
Founder & President <http://www.instantinteractive.com>

<mailto:email@hidden>
------------------------------------------------------------


  • Follow-Ups:
    • Re: Error in learning Cocoa book?
      • From: Ondra Cada <email@hidden>
    • Re: Error in learning Cocoa book?
      • From: Brendan Younger <email@hidden>
  • Prev by Date: Re: NSAlertPanel.beginAlertSheet changes
  • Next by Date: Re: Question Mark
  • Previous by thread: Re: NSAlertPanel.beginAlertSheet changes
  • Next by thread: Re: Error in learning Cocoa book?
  • Index(es):
    • Date
    • Thread