Ramy Al Zuhouri
Ramy Al Zuhouri

Reputation: 21966

It fails to copy a string to pasteboard

I have a string called text, in the main menu xib file I linked the copy menu item with an action called through an observer.I verified with a breakpoint that this method is actually called, but the problem is that the string isn't really copied to the pasteboard:

- (void) copy: (NSNotification*) notification
{
    if([[self window]isKeyWindow])
    {
        // It always enters in this block
        NSPasteboard* pb=[NSPasteboard generalPasteboard];
        NSPasteboardItem* item=[[NSPasteboardItem alloc]init];
        [pb clearContents];
        [item setData: [NSKeyedArchiver archivedDataWithRootObject: text] forType: NSPasteboardTypeString];
        [pb writeObjects: [NSArray arrayWithObject: item]];
    }
}

After entering in the block it clear all the contents of the pasteboard.But if I try to paste the copied content to text edit, it doesn't paste anything (an empty string), but the string is not of length zero.
I also tried to check the return value of writeObjects, and it returns YES.

Upvotes: 0

Views: 460

Answers (1)

Tommy
Tommy

Reputation: 100622

You can simply use:

[pb setString:text forType:NSPasteboardTypeString];

Alternatively you probably want to use setString:forType on NSPasteboardItem.

Upvotes: 2

Related Questions