Nicolas Charvoz
Nicolas Charvoz

Reputation: 1509

Create a class named with a Swift-used name

I need to call my class is, but when I try to do this:

class is {

}

I get an error:

Expected identifier in class declaration

Is there a way to bypass this error and call my class is? I don't want to call it IS or iss or iis but is .

Upvotes: 1

Views: 136

Answers (1)

jtbandes
jtbandes

Reputation: 118651

I wouldn't advise this — it'll certainly make your code much less readable, and there are simple alternatives (such as just using a non-reserved word).

But if you really want, you can write the name as `is`. Escaping with backticks allows you to use reserved words / keywords as identifiers. You'll have to use the backticks everywhere you want to use the class name:

class `is` { }

let obj = `is`()

let objs: [`is`] = []

More information can be found in The Swift Programming Language.

Upvotes: 6

Related Questions