2016-05-05 5 views
0

SO,Convertir Unicode échappé en unicode dans Objective-c

Une question apparemment simple m'a étonné. J'ai deux déclarations:

NSLog(@"%@", @"\U0001f1ee\U0001f1f9"); 

NSLog(@"%@", @"\\U0001f1ee\\U0001f1f9"); 

Le premier sort l'emoji correct (drapeau). La seconde renvoie une chaîne échappée. Quelle conversion ai-je besoin de faire à la deuxième chaîne pour qu'elle produise également le drapeau? En d'autres termes: J'ai des chaînes d'Unicode échappé que je veux imprimer comme Emoji propre. Comment ferais-je cela?

J'ai essayé de convertir en NSUTF8StringEncoding NSData puis de nouveau à NSString, j'ai essayé d'utiliser NSNonLossyASCIIStringEncoding, pas de joie. Je dois les utiliser mal ...

Merci pour toute aide!

+0

veuillez essayer NSString * name2escaped = @ "\ U0001f1ee \ U0001f1f9"; NSString * name2 = [NSString stringWithCString: [nom2escapé cStringUsingEncoding: NSUTF8StringEncoding] codage: NSNonLossyASCIIStringEncoding]; NSLog (@ "nom2 =% @", nom2); – Rajesh

+0

Cela ne fonctionne pas. J'ai déjà vu ce poste. :( –

+0

s'il vous plaît essayer ce peut-être résoudre votre problème NSString * entrée = @ "\ U0001f1ee \ U0001f1f9" "; NSString * convertedString = [entrée mutableCopy]; CFStringRef transformer = RRACC (" Any-Hex/Java "); CFStringTransform ((__ bridge CFMutableStringRef) convertedString, NULL, transformer, YES); NSLog (@ "convertedString:% @", convertedString); – Rajesh

Répondre

0

Facile. Utilisez -stringByRemovingPercentEncoding.

NSString * string = @"\\U0001f1ee\\U0001f1f9" ; 
NSLog(@"%@", [string stringByRemovingPercentEncoding]); 
+0

Hmm cela ne semble pas fonctionner pour moi. Il imprime '' '' '' \ U0001f1ee \ U0001f1f9''' –