0

I'm trying to create an NSMutableArray of characters.

lowerCaseLetters = [NSMutableArray new];
for (char crt = 'a'; crt <= 'z'; crt ++) {
    NSString *chrStr = [NSString stringWithCString:&crt encoding:NSUTF8StringEncoding];
    [lowerCaseLetters addObject:chrStr];
}
NSLog(@"%@",lowerCaseLetters);

Result:

  "a@Ip",
    "b@Ip",
    "c@Ip",
    "d@Ip",
    "e@Ip",
    "f@Ip",
    "g@Ip",
    "h@Ip",
    "i@Ip",
    "j@Ip",
    "k@Ip",
    "l@Ip",
    "m@Ip",
    "n@Ip",
    "o@Ip",
    "p@Ip",
    "q@Ip",
    "r@Ip",
    "s@Ip",
    "t@Ip",
    "u@Ip",
    "v@Ip",
    "w@Ip",
    "x@Ip",
    "y@Ip",
    "z@Ip"
)

Why do I get this? Is there a better way to do this? PS: Sometimes this crashes with "insertObject:atIndex:" can not insert nil object.... Why?

3
  • A "character string" is supposed to be null-terminated. Yours isn't, so stringWithCString blindly reads ahead until it encounters a random zero byte. Commented Jan 1, 2014 at 14:18
  • The single quotes are used to create character constants, double quotes are used to create strings that are nu terminated. Commented Jan 1, 2014 at 14:20
  • Keep in mind that "a-z" as lowercase letters is only valid for some languages. Other languages have either additional or completely different lowercase letters. Keep that in mind when you consider that your app may be used by people from all over the world. Commented Jan 1, 2014 at 20:26

3 Answers 3

2

This is undefined behavior:

NSString *chrStr = [NSString stringWithCString:&crt encoding:NSUTF8StringEncoding];

The problem is that &crt is not a C string, because C strings must be null-terminated. You can fix it like this:

char buf[2];
buf[0] = crt;
buf[1] = '\0';
NSString *chrStr = [NSString stringWithCString:buf encoding:NSUTF8StringEncoding];

You could also use stringWithFormat: for a simpler approach, like this:

NSString *chrStr = [NSString stringWithFormat:@"%c", crt];
Sign up to request clarification or add additional context in comments.

1 Comment

The last approach looks really nice!
1

Following on from dasblinkenlight, another approach is

for (unichar crt = 'a'; crt <= 'z'; crt ++) // note crt is now unichar
{
    NSString *chrStr = [NSString stringWithCharacters: &crt length: 1];
    [lowerCaseLetters addObject:chrStr];
}

Following on from user2734323 another approach is

lowerCaseLetters = [@[ @"a", @"b", @"c",  .... , @"x", @"y", @"z" ] mutableCopy];

Comments

1

Try this...

NSString *stringWithComma = @"a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z";
NSArray *lowerCaseLetters = [[NSArray new] init];

lowerCaseLetters = [stringWithComma componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@","]];
NSLog(@"Array %@",lowerCaseLetters);

This gives you an NSArray. If you need a NSMutableArray, you have to copy from the NSArray.

or simply allocate

NSMutableArray *lowerCaseLetters = [[NSMutableArray alloc] initWithObjects: @"a", @"b",...,@"z", nil]; 

The first approach is dynamic though as you could create stringWithComma dynamically with any values in it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.