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

Messages on command prompt

batch-file cmd

I want to show the result of my function as a pop up message.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.