copyWithZone being called

So i have 2 Objects.

Library.h:
   NSString *name;
   Book *book;

Book.h:
   NSString *title
   NSString *author;

The properties are all set up as (readwrite,copy)

ViewController.h:
   Library *library;
   Book *book;

ViewController.m:
   library = [[Library alloc] init];
   book = [[Book alloc]init];


  //The fallowing all works
  library.name = @"Library Name";
  book.title = @"book Title";
  book.author = @"book author";

 //The fallowing crashes my app
 library.book = book;

How can I encapsulate Book inside of the library object?

The debugger gives me the fallowing error.

[library copyWithZone:]: unrecognized selector

Answers


The -copyWithZone: selector is being called because you're assigning an object to a property that is marked as (copy). Marking it as (copy) tells the compiler to create a setter on your behalf that tries to make a copy of the object being assigned.

You either need to mark your properties as (readwrite, retain), so that you retain the existing instance of Book instead of making a copy, or you need to implement the NSCopying protocol for your Book class. Here's Apple's documentation on NSCopying:

http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Protocols/NSCopying_Protocol/Reference/Reference.html%23//apple_ref/doc/uid/TP40003777

Whether you change the property attributes or adopt the NSCopying protocol depends on whether you truly need to copy the object being assigned (usually only necessary when the object being assigned is modifiable and may be changed by the caller or someone else), or can get away with just retaining a reference to the existing object (considered to be the normal case).


Need Your Help

Visual Studio 2008 and .NET Framework 4.0

visual-studio-2008

If I have Visual Studio 2008 and .NET framework 4.0, can I run a project or application built using Visual Studio 2010?

Should I use /* */ or /** */ for the copyright in the top of a java file?

java coding-style comments javadoc copyright-display

There is a copyright comment in each java file, but I don't know which one should I use: /* */ or /** */?

Need to use matplotlib scatter markers outside the chart, in labels for a bar graph

python charts matplotlib

I have a scatter plot that uses six different symbols to plot points. For example, the code below plots a point with the downward-pointing triangle symbol: