2011-04-11 2 views
0

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]; 
} 

Répondre

1

En supposant que vos identifiants sont structurés comme

<lowercase-prefix><Uppercase-char-and-remainder> 

vous pouvez utiliser:

NSScaner *scanner = [NSScanner scannerWithString:fieldName]; 
NSString *prefix = nil; 
[scanner scanCharactersFromSet:[NSCharacterSet lowercaseLetterCharacterSet] intoString:&prefix]; 
NSString *suffix = nil; 
[scanner scanCharactersFromSet:[NSCharacterSet letterCharacterSet] intoString:&suffix]; 
NSString *fieldNameString = [NSString stringWithFormat:@"%@_%@", prefix, [suffix lowercaseString]]; 

Ce serait effectuer la conversion des identificateurs de champ (mais vous devez effectuer une vérification d'erreur en cas le préfixe ou le suffixe reste nul).

La meilleure façon de construire la liste des FIELDNAMES serait de les ajouter à une NSMutableArray, et puis les rejoindre:

NSMutableArray *fields = [[NSMutableArray alloc] init]; 
for (NSString *fieldName in [self fieldList]) { 
    // code as above 
    [fields add:fieldNameString]; 
} 
NSString *commaFields = [fields componentsJoinedByString:@","]; 
[fields release]; 
return commaFields; 
+0

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