开发者

What's the difference between single and double quotes in Objective-C?

I'm working through a book exercise to generate some random serial number, and here's my function:

NSString *randomSerialNumber = [NSString stringWithFormat:@"%c%c%c%c%c",
                                    '0' + random() % 10,
                                    'A' + random() % 26,
                                    '0' + random() % 10,
                                    'A' + random() % 26,
                                    '0' + random() % 10];

This works and has an output like: 2J6X7. But before, the 0s and As I had wrapped in double quotes, and an e开发者_JAVA技巧xample output was 11764ıÒ˜. What did I do wrong my first time around, and why did using single quotes fix it?


The difference between single and double quotes is that double quotes declare a string, and single quotes declare a single character. Try doing this, you will get a syntax error:

'More than one character'

The reason that your code outputted a bunch of random characters is that strings are not integers like most other data types, but pointers. This means that when you type "A string", the result of the expression is the memory location that the characters are stored at. This could be anywhere in memory, depending on when you start the program. So, when you added random() to the string, it gave you a random memory address! The statement was equivalent to this in English:

Store the characters "A" in memory, and then give me the memory
address a random amount of cells later.

The random amount of cells later could be anything else in your program. The pointer was interpreted as an character (because of %c), but it wasn't meant to, giving you seemingly random output.


The single quotes are giving you an ASCII representation of each character (char), to which you are adding a number. The double quotes give you a string (char *), to which adding a number really doesn't make much sense.

I might be wrong here, but I think at least the first half is right!


Single quotes are used for single character constants. Double quotes are used for strings, either C strings or NSStrings (which have '@' prefixes). When you used double quotes, the + meant pointer arithmetic. The resulting pointers were passed where characters were expected, which leads to undefined behavior. The correct version does integer arithmetic on the chars, which works as expected.


The numbers that you use for modulus controls how many ASCII characters after the one specified within the single quotes that can show up in the final string. Take a look at this table: http://www.asciitable.com/, for a reference on the ASCII numbers.

'0' + random() % 10 will give you characters from '0' to '10'

'A' + random() % 26 will give you characters from 'A' to 'Z'

Apologies if this is really obvious..

I'd like to add a question to this: is there a nice way to generate a similar string using regular expressions instead?


Adrian's answer is right. I'm just trying to add clarification.

"A" does three things. It reserves space for 2 characters, the A and a nul terminator. It puts the codes which represent those two characters in that space. It returns the address of the first one. That address is of the type: char *

'A' just returns the value of the code for A, the same value as was put in the first memory cell above. the value is of type char.

So as you can see there is a big difference.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜