How to convert text to camel body in Objective-C?

I am doing little usefulness to help me generate code for the application that I am creating. I like to have constants for my NSUserDefaults settings, so my code is more readable and easier to maintain. The problem is that it takes some time to create constants for everything, so I'm trying to write a utility to generate the code for me. I would like to be able to enter a string and convert it to a camel, for example:

- (NSString *)camelCaseFromString:(NSString *)input{
    return inputAsCamelCase;
}

Now the input line can consist of several words. I guess I need some kind of regular expression here, or maybe there is another way to do this. I would like to introduce something like this:

@"scrolling direction"

or that:

@"speed of scrolling"

and return something like this:

kScrollingDirection

or that:

kSpeedOfScrolling

, , ?

+5
3
- (NSString *)camelCaseFromString:(NSString *)input
{
    return [NSString stringWithFormat:@"k%@", [[input capitializedString]stringByReplacingOccurrencesOfString:@" " withString:@""]];
}
  • .
  • .
  • "k" . ( , stringWithFormat.)
+13

. ( @jaydee3)

, , PascalCasing TLA, "" , capitalizedString .

, "I prefer camelCasing to PascalCasing for variables" "iPreferCamel c asingToPascalcasingForVariables", "iPreferCamel C asingToPascalCasingForVariables"

NSString . / ALL_CAPS , orig.

camelCased . "WTH" "WTH", . , , . , "k", "k IBM" "kIBM", , "kIBM" .

:

NSString *str = @"K computer manufacturer IBM";
NSLog(@"constant: %@", str.pascalCased);

// "constant:  kComputerManufacturerIBM"

( ).

@implementation NSString (MixedCasing)

- (NSString *)camelCased  {    
    NSMutableString *result = [NSMutableString new];
    NSArray *words = [self componentsSeparatedByString: @" "];
    for (uint i = 0; i < words.count; i++) {
        if (i==0) {
            [result appendString:((NSString *) words[i]).withLowercasedFirstChar];
        }
        else {
            [result appendString:((NSString *)words[i]).withUppercasedFirstChar];
        }
    }
    return result;
}

- (NSString *)pascalCased  {    
    NSMutableString *result = [NSMutableString new];
    NSArray *words = [self componentsSeparatedByString: @" "];
    for (NSString *word in words) {
        [result appendString:word.withUppercasedFirstChar];
    }
    return result;
}

- (NSString *)withUppercasedFirstChar  {    
    if (self.length <= 1) {
        return self.uppercaseString;
    } else {
        return [NSString stringWithFormat:@"%@%@",[[self substringToIndex:1] uppercaseString],[self substringFromIndex:1]];
    }
}

- (NSString *)withLowercasedFirstChar {    
    if (self.length <= 1) {
        return self.lowercaseString;
    } else {
        return [NSString stringWithFormat:@"%@%@",[[self substringToIndex:1] lowercaseString],[self substringFromIndex:1]];
    }
}
@end
+1

: @ " ".capitalizedString; : → " "

...

The top of the first word is char of the word and omits the other letters for each word.

+1
source

All Articles