iOS 9.2.1, Xcode 7.2.1, ARC abilitato
È sempre possibile aggiungere il '%' da solo, senza altri identificatori di formato nella stringa che si sta aggiungendo, in questo modo ...
int test = 10;
NSString *stringTest = [NSString stringWithFormat:@"%d", test];
stringTest = [stringTest stringByAppendingString:@"%"];
NSLog(@"%@", stringTest);
per iOS7.0 +
per espandere la risposta ad altri personaggi che potrebbero causare dei conflitti si può scegliere di utilizzare:
- (NSString *)stringByAddingPercentEncodingWithAllowedCharacters:(NSCharacterSet *)allowedCharacters
Scritto passo dopo passo, che appare così:
int test = 10;
NSString *stringTest = [NSString stringWithFormat:@"%d", test];
stringTest = [[stringTest stringByAppendingString:@"%"]
stringByAddingPercentEncodingWithAllowedCharacters:
[NSCharacterSet alphanumericCharacterSet]];
stringTest = [stringTest stringByRemovingPercentEncoding];
NSLog(@"percent value of test: %@", stringTest);
o corto mano:
NSLog(@"percent value of test: %@", [[[[NSString stringWithFormat:@"%d", test]
stringByAppendingString:@"%"] stringByAddingPercentEncodingWithAllowedCharacters:
[NSCharacterSet alphanumericCharacterSet]] stringByRemovingPercentEncoding]);
Grazie a tutti i collaboratori originali. Spero che questo ti aiuti. Saluti!
fonte
2016-02-09 17:30:05
Sto provando NSLog ([NSString stringWithFormat: @ "% @ %%", self.Ptextfield.text]); e l'output è jsut theuitextfield.text – Ali
'NSLog' tratta il suo primo argomento come una stringa di formato, ma hai già formattato la stringa usando' stringWithFormat: '. Dì solo "NSLog (@"% @ %% ", self.Ptextfield.text)'. –
Questo non funziona per UILocalNotification. Vedi http://stackoverflow.com/a/27971848/2446178. –