Chapter 21, Challenge 1


#1

I implemented Challenge 1 using Lazy Copying. I also did a snapshot of the data as noted in the book and stored it in a class instance variable. Using ARC, what would be the proper way to release the snapshot memory when the pasteboardFinishedWithDataProvider method is called? Should it be stored somewhere other than a class instance variable?


#2

Under ARC the way to release a reference is to assign it to nil, so just set the snapshot instance variable to nil in pasteboardFinishedWithDataProvider:! :slight_smile:


#3

That’s easy enough. Does this work even if you don’t go through an accessor?

So:

snapshot = nil;
or
[self setSnapshot: nil];

would both release memory?


#4

Yep!

You may recall that ARC doesn’t allow Objective-C object pointers inside of C structs… this is part of the reason. It can monitor what you’re doing to instance variables (like it is here to release the object when you set the ivar to nil), but it can’t monitor C struct members in the same way.


#5

I have a question regarding this Challenge. Is it valid to mix NSPasteboardItem with “normal” objects like this? :

[item setData:[self dataWithPDFInsideRect:[self bounds]] forType:NSPasteboardTypePDF]; [pb writeObjects:[NSArray arrayWithObject:item]]; [pb writeObjects:[NSArray arrayWithObject:string]];
btw, I do not understand that the Apple docs do not contain in the method description e.g. setData:forType: a reference to special parameter types like NSPasteboardTypePDF etc. - If I had not read the book, I would feel completely lost at that point - even in the related programming guide I couldn’t find any hint of this group of constants :frowning:


#6

Yes, you can mix like that. Keep in mind that each object you write is intended as a separate item on the pasteboard. Each item can have multiple representations (string, attributed string, PDF, PNG). If you want to set those multiple representations for one item, you would need to set them on one NSPasteboardItem, then write that pasteboard item to the pasteboard.

Adam