Il metodo si citano legge un file dal disco con una data codifica dei caratteri (come UTF-8 o ASCII). Non ha nulla a che fare con l'escape dell'URL o dell'HTML.
Se si desidera aggiungere fughe URL per cento, si desidera che questo metodo:
[myString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
Assicurarsi di leggere la documentazione su questo metodo, perché ci sono alcune sottigliezze su ciò che sfugge e ciò che lascia soli. In alcuni casi, potrebbe essere necessario utilizzare il più complesso, ma più flessibile, CFURLCreateStringByAddingPercentEscapes()
. (Se lo fai, si noti che si può lanciare CFStringRef
-NSString *
e viceversa.)
Non c'è niente di costruito in che io sappia fare entità XML/HTML in stile fuga, ma questa funzione dovrebbe gestire le basi:
NSString * convertToXMLEntities(NSString * myString) {
NSMutableString * temp = [myString mutableCopy];
[temp replaceOccurrencesOfString:@"&"
withString:@"&"
options:0
range:NSMakeRange(0, [temp length])];
[temp replaceOccurrencesOfString:@"<"
withString:@"<"
options:0
range:NSMakeRange(0, [temp length])];
[temp replaceOccurrencesOfString:@">"
withString:@">"
options:0
range:NSMakeRange(0, [temp length])];
[temp replaceOccurrencesOfString:@"\""
withString:@"""
options:0
range:NSMakeRange(0, [temp length])];
[temp replaceOccurrencesOfString:@"'"
withString:@"'"
options:0
range:NSMakeRange(0, [temp length])];
return [temp autorelease];
}
fonte
2009-11-03 15:48:24
Leggi le altre risposte per i metodi per quotare stringhe come XML (ad esempio, sostituzione dell'entità e