First challenge

I’ve been struggling with this one for the last couple of days. Unfortunately, I’ve been unable to find any useful information on the web that would lead me to a solution using the method described in the hint. Here is the text of the challenge:

Challenge: Write Multiple Representations – You are putting the string onto the pasteboard. Create the PDF for the view and put that on the pasteboard, too. Now you will be able to copy the image of the die into graphics programs. Test it using Preview’s New from Clipboard menu item. (Do not break the string’s copy and paste: Put both the string and the PDF onto the pasteboard.) Hint: You will need to create an NSPasteboardItem.

I’m understanding this to mean with a single copy to pasteboard, both the textual digit and the graphical representation of the die should be captured. I’m able to get one or the other, but not both with the method I used, which isn’t what was suggested:

[code]func writeToPasteboard(pasteboard: NSPasteboard) {

    if let intValue = intValue {
        pasteboard.clearContents()
        pasteboard.writeObjects(["\(intValue)"])
        
        // Challenge code
        // Can't get both the string & the PDF image to copy!
        
        pasteboard.clearContents()  // Key #1
        let newPDFValue = self.dataWithPDFInsideRect(self.bounds)
        pasteboard.setData(newPDFValue, forType: NSPasteboardTypePDF)
    }
    
}[/code]

If I don’t clear the pasteboard contents the second time (Key #1), only the textual digit is copied; with the second clear, only the graphic is copied.

I’d love to be able to figure this out since I’ve invested so much time in this. The book has some tantalizing clues, but a full-on example of the whole process would be useful.

Thanks!

Tom

This one took me a while to figure out but once I did I was surprised that is was less difficult than I was originally trying to make it. Have you figured it out yet?

Remember, you have to put the pdf representation inside an NSPasteboardItem. Inside writeToPasteBoard(: NSPasteboard) create a new NSData object with the pdf representation and an NSPasteboardItem object. Use the setData(, forType: String) method and add the pasteboardItem object to writeObjects([AnyObject]) list of objects to be put on the pasteboard.

Thanks, Brian. I moved on to subsequent chapters after giving up … this time. I’m going to be going through the entire book a second time and will be focusing on the challenges with a goal to complete them all (I finished about 1/3 of the ones I tried).

Thanks for the tip, though. I’ll post my results when I get to this one again. :slight_smile:

All righty then …

I’m back up to this chapter and tried the challenge again. After a couple of false starts, I finally figured it out (thanks in part to your hints, Brian … thanks!). Yeah, it was pretty easy after I thought about it awhile and reread the challenge a couple of dozen times :slight_smile:.

WARNING: Spoiler -

    func writeToPasteboard(pasteboard: NSPasteboard) {
        if let intValue = intValue {
            pasteboard.clearContents()
            pasteboard.writeObjects(["\(intValue)"])

            let data: NSData = self.dataWithPDFInsideRect(self.bounds)
            let pdfPasteboardItem = NSPasteboardItem()
            pasteboard.setData(data, forType: NSPasteboardTypePDF)
            pasteboard.writeObjects([pdfPasteboardItem])
        }
    }

You could have written both values add once

            pasteboard.writeObjects(["\(intValue)", pdfPasteboardItem])

What I’ve seen so far puts two items in the pasteboard, each with one aspect. I thought we should have a single item that happens to have two aspects. The text and image representations have to be part of the same item, not two separate ones.

    func writeToPasteboard(pasteboard: NSPasteboard) {
        if let intValue = intValue {
            let pbItem = NSPasteboardItem()

            pbItem.setData(self.dataWithPDFInsideRect(self.bounds), forType: kUTTypePDF as String)
            pbItem.setString("\(intValue)", forType: kUTTypeUTF8PlainText as String)

            pasteboard.clearContents()
            pasteboard.writeObjects([pbItem])
        }
    }

You can’t leave the string representation code untouched; you have to put it as a separate aspect of the same NSPasteboardItem that the PDF representation will go in.