Marcos Crispino
Marcos Crispino

Reputation: 8218

Valid characters in a class name in Objective-C

What is the set of valid characters for an Objective-C class?

Of course the basic naming conventions of letters, numbers and underscore all work, but I'm looking for a special character to avoid naming conflicts.

The $ symbol seems to work, but I could not find any documentation. The language reference does not mention it.

Any other? Any "official" document I can refer to?

Upvotes: 14

Views: 1646

Answers (2)

Greg Parker
Greg Parker

Reputation: 8012

At compile time, the set of characters is whatever the C/C++ compiler accepts as an identifier.

At runtime, anything UTF-8 goes.

Upvotes: 3

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726479

Historically, Objective-C compiler has been based on GNU C compiler, which allowed dollar signs in identifiers as an extension. As far as I know, this is the only exception to the rule of naming identifiers with sequences of letters, digits, and underscores that do not start in a digit.

Since Apple switched to a different toolchain for their compilers, it appears that the dollar sign is kept for backward compatibility. I would avoid using it in my new code, especially since Apple is not advertising it as a feature.

Upvotes: 4

Related Questions