J'essaie d'écrire une méthode générique pour la conversion de chaîne (le but étant d'écrire un analyseur pour une API RESTful).Remplacement de chaîne complexe Objective-C
Le message vise à convertir des chaînes comme suit
creationTSZ -> creation_tsz
userId -> user_id
Le message poignées conversion userId -> user_id, boucle actuellement inefficacement par la chaîne et de changer les pièces .
Il ne gère pas encore la créationTSZ -> creation_tsz, je pense que le bouclage est très inefficace, et je me demande s'il y a une meilleure façon de le faire?
Peut-être Regex?
-(NSString *)fieldsQueryString
{
NSArray *fieldNames = [self fieldList];
/* Final composed string sent to Etsy */
NSMutableString *fieldString = [[[NSMutableString alloc] init] autorelease];
/* Characters that we replace with _lowerCase */
NSArray *replaceableChars = [NSArray arrayWithObjects:
@"Q", @"W", @"E", @"R", @"T", @"Y", @"U", @"I", @"O", @"P",
@"A", @"S", @"D", @"F", @"G", @"H", @"J", @"K", @"L",
@"Z", @"X", @"C", @"V", @"B", @"N", @"M", nil];
/* Reusable pointer for string replacements */
NSMutableString *fieldNameString = nil;
/* Loop through the array returned by the filter and change the names */
for(NSString *fieldName in fieldNames) {
/* Loop if the field is to be omited */
if ([[self valueForKey:fieldName] boolValue] == NO) continue;
/* Otherwise change the name to a field and add it */
fieldNameString = [fieldName mutableCopy];
for(NSString *replaceableChar in replaceableChars) {
[fieldNameString replaceOccurrencesOfString:replaceableChar
withString:[NSString stringWithFormat:@"_%@", [replaceableChar lowercaseString]]
options:0
range:NSMakeRange(0, [fieldNameString length])];
}
[fieldString appendFormat:@"%@,", fieldNameString];
[fieldNameString release];
}
fieldNames = nil;
/* Return the string without the last comma */
return [fieldString substringToIndex:[fieldString length] - 1];
}
Certaines œuvres une chaîne comme creationTSZ, ne peut évidemment pas analyser les cas multiples de chameau par exemple thisStringHasAnotherCapitalLetter . J'essaierai de travailler avec NSScanner pour voir si cela fonctionne. Merci de m'avoir indiqué la bonne direction. – Devraj