开发者

How to declare local "variables" as final in Objective-C?

开发者 https://www.devze.com 2022-12-19 10:57 出处:网络
In Java, I can declare local \"variables\" as final, e.g. void myMethod() { final String foo = \"Hello World!\";

In Java, I can declare local "variables" as final, e.g.

void myMethod() {
    final String foo = "Hello World!";
    foo = "Bye-bye..."; // compile error!!
    return;开发者_JAVA百科
}

When I try to change its value, a get an error from the compiler. I want to declare some of my local "variables" final to avoid changing their value by accident.

Is this possible in Objective-C?


Like in C/C++, you can use the const keyword.

For more info, this article seems right up your alley: Java Developer’s Guide to String Constants in Objective-C

0

精彩评论

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