views:

86

answers:

2

In this function, I get the selected emoticon from NSTableView from the NSArrayController connected to an IBOutlet called emotes. The string in the selected NSArray contains UTF8 characters that is sent to the clipboard.

// Get Selected Emoticon
NSArray * selectedemote = [emotes selectedObjects];
NSLog(@"%@",[selectedemote valueForKey:@"emote"]);
// Send to Clipboard
[self copyToClipboard:[NSString stringWithFormat:@"%@",[selectedemote valueForKey:@"emote"]]];

The problem is with the NSArray itself. It seems to output the UTF8 string as a hexadecimal. Here is what the string looks like from the NSLog function:

2010-08-23 11:23:56.411 Emoticon[7919:a0f] (
    "\\(\U2579\U30ee\U2579 )/"
)

Is there a possible way of converting the UTF-8 hex to a UTF-8 characters? Thanks.

A: 

The problem is with the NSArray itself. It seems to output the UTF8 string as a hexadecimal.

As I noted in my comment, the string is not UTF-8. Strings have nothing to do with encodings, except when you create them from data or create data from them.

Here is what the string looks like from the NSLog function:

2010-08-23 11:23:56.411 Emoticon[7919:a0f] (
    "\\(\U2579\U30ee\U2579 )/"
)

NSArray describes itself by returning a string in old-style property-list format. That format must be ASCII, so the array escapes the non-ASCII characters of the string with those \U sequences. This is working as it's supposed to, and it's specific to the array's description. The string remains unchanged, as you'll see if you log the string directly.

If you want to put the array on the clipboard, just do that. You should make up a uniform type identifier (yourdomain.yourappname.typename) and tell the pasteboard to set the array as the property-list value for that type. You can also tell the array to join the strings using some separator (e.g., @"\n") and put that string on the pasteboard under the string type, for easy pasting into text editors and text fields.

Peter Hosey
That explains the problem, but still not a solution to fix the problem. I'll try and figure it out myself.
chikorita157
chikorita157: No, it explains that there is no problem. The string is not escaped the way the array shows it; the array does the escaping only in its description of itself. As I said, instead of logging the array, log the string directly. Then you will see the string's true contents.
Peter Hosey
A: 

I solved the issue. The solution is to use the NSEntityDescription and init it with the objectAtIndex:0 in the NSArray with the selected cell. Using the NSEntityDescription, I got the value for Emote.

-(IBAction)sendtoclipboard:(id)sender
{
    // Get Selected Emoticon
    NSArray * selectedemote = [emotes selectedObjects];
    NSEntityDescription *entity = [selectedemote objectAtIndex:0];
    NSString * tmpemote = [NSString stringWithFormat:@"%@",[entity valueForKey:@"emote"]];
    NSLog(@"%@", [entity valueForKey:@"emote"]);
    //Send to Clipboard
    [self copyToClipboard:tmpemote];
}

The result:

2010-09-07 20:03:26.488 Emoticon[45764:a0f] \(╹ヮ╹ )/
chikorita157